• 検索結果がありません。

マルチパートアップロードを 使用したアーカイブのアップロード使用したアーカイブのアップロード

ドキュメント内 Amazon S3 Glacier - 開発者ガイド (ページ 181-200)

マルチパートアップロードでアーカイブをアップロードする場合の木構造ハッシュの計算のプロセスは、

単一のリクエストでアーカイブをアップロードする場合と同じです。唯一の違いは、 (パートのアップ ロード (PUT uploadID) (p. 255) API を使用して) 各リクエストでアーカイブのパートを 1 つのみアップ ロードする点です。したがって、そのパートのチェックサムのみを x-amz-sha256-tree-hash リクエ ストヘッダーに含めます。ただし、すべてのパートをアップロードした後で、 マルチパートアップロード の完了 (POST uploadID) (p. 238)リクエストヘッダーにアーカイブ全体の木構造ハッシュを含めたマルチ

ファイルの木構造ハッシュの計算

パートアップロードの完了 (「x-amz-sha256-tree-hash」を参照) リクエストを送信する必要がありま す。

ファイルの木構造ハッシュの計算

以下に示すアルゴリズムは、デモンストレーションのために選択したものです。実装シナリオでは、必 要に応じてコードを最適化できます。使用している場合AWSSDK を使用して Amazon S3 Glacier (S3 Glacier) に対してプログラミングする場合は、木構造ハッシュの計算がお客様のために行われるため、必 要な作業はファイルの参照を指定することのみです。

Example 1: Java の例

以下の例は、Java を使用してファイルの SHA256 木構造ハッシュを計算する方法を 示しています。この例は、ファイルの場所を引数として指定するか、コードから直接 TreeHashExample.computeSHA256TreeHash メソッドを使用することで実行できます。

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

public class TreeHashExample {

static final int ONE_MB = 1024 * 1024;

/**

* Compute the Hex representation of the SHA-256 tree hash for the specified * File

*

* @param args

* args[0]: a file to compute a SHA-256 tree hash for */

public static void main(String[] args) {

ファイルの木構造ハッシュの計算

if (args.length < 1) {

System.err.println("Missing required filename argument");

System.exit(-1);

}

File inputFile = new File(args[0]);

try {

byte[] treeHash = computeSHA256TreeHash(inputFile);

System.out.printf("SHA-256 Tree Hash = %s\n", toHex(treeHash));

} catch (IOException ioe) {

System.err.format("Exception when reading from file %s: %s", inputFile, ioe.getMessage());

System.exit(-1);

} catch (NoSuchAlgorithmException nsae) {

System.err.format("Cannot locate MessageDigest algorithm for SHA-256: %s", nsae.getMessage());

System.exit(-1);

} } /**

* Computes the SHA-256 tree hash for the given file *

* @param inputFile

* a File to compute the SHA-256 tree hash for * @return a byte[] containing the SHA-256 tree hash * @throws IOException

* Thrown if there's an issue reading the input file * @throws NoSuchAlgorithmException

*/

public static byte[] computeSHA256TreeHash(File inputFile) throws IOException, NoSuchAlgorithmException {

byte[][] chunkSHA256Hashes = getChunkSHA256Hashes(inputFile);

return computeSHA256TreeHash(chunkSHA256Hashes);

} /**

* Computes a SHA256 checksum for each 1 MB chunk of the input file. This * includes the checksum for the last chunk even if it is smaller than 1 MB.

*

* @param file

* A file to compute checksums on

* @return a byte[][] containing the checksums of each 1 MB chunk * @throws IOException

* Thrown if there's an IOException when reading the file * @throws NoSuchAlgorithmException

* Thrown if SHA-256 MessageDigest can't be found */

public static byte[][] getChunkSHA256Hashes(File file) throws IOException, NoSuchAlgorithmException {

MessageDigest md = MessageDigest.getInstance("SHA-256");

long numChunks = file.length() / ONE_MB;

if (file.length() % ONE_MB > 0) { numChunks++;

}

if (numChunks == 0) {

return new byte[][] { md.digest() };

}

ファイルの木構造ハッシュの計算

byte[][] chunkSHA256Hashes = new byte[(int) numChunks][];

FileInputStream fileStream = null;

try {

fileStream = new FileInputStream(file);

byte[] buff = new byte[ONE_MB];

int bytesRead;

int idx = 0;

while ((bytesRead = fileStream.read(buff, 0, ONE_MB)) > 0) { md.reset();

md.update(buff, 0, bytesRead);

chunkSHA256Hashes[idx++] = md.digest();

}

return chunkSHA256Hashes;

} finally {

if (fileStream != null) { try {

fileStream.close();

} catch (IOException ioe) {

System.err.printf("Exception while closing %s.\n %s", file.getName(), ioe.getMessage());

} } } } /**

* Computes the SHA-256 tree hash for the passed array of 1 MB chunk * checksums.

*

* This method uses a pair of arrays to iteratively compute the tree hash * level by level. Each iteration takes two adjacent elements from the * previous level source array, computes the SHA-256 hash on their

* concatenated value and places the result in the next level's destination * array. At the end of an iteration, the destination array becomes the * source array for the next level.

*

* @param chunkSHA256Hashes

* An array of SHA-256 checksums

* @return A byte[] containing the SHA-256 tree hash for the input chunks * @throws NoSuchAlgorithmException

* Thrown if SHA-256 MessageDigest can't be found */

public static byte[] computeSHA256TreeHash(byte[][] chunkSHA256Hashes) throws NoSuchAlgorithmException {

MessageDigest md = MessageDigest.getInstance("SHA-256");

byte[][] prevLvlHashes = chunkSHA256Hashes;

while (prevLvlHashes.length > 1) { int len = prevLvlHashes.length / 2;

if (prevLvlHashes.length % 2 != 0) { len++;

}

byte[][] currLvlHashes = new byte[len][];

int j = 0;

for (int i = 0; i < prevLvlHashes.length; i = i + 2, j++) {

ファイルの木構造ハッシュの計算

// If there are at least two elements remaining if (prevLvlHashes.length - i > 1) {

// Calculate a digest of the concatenated nodes md.reset();

md.update(prevLvlHashes[i]);

md.update(prevLvlHashes[i + 1]);

currLvlHashes[j] = md.digest();

} else { // Take care of remaining odd chunk currLvlHashes[j] = prevLvlHashes[i];

} }

prevLvlHashes = currLvlHashes;

}

return prevLvlHashes[0];

} /**

* Returns the hexadecimal representation of the input byte array *

* @param data

* a byte[] to convert to Hex characters * @return A String containing Hex characters */

public static String toHex(byte[] data) {

StringBuilder sb = new StringBuilder(data.length * 2);

for (int i = 0; i < data.length; i++) {

String hex = Integer.toHexString(data[i] & 0xFF);

if (hex.length() == 1) { // Append leading zero.

sb.append("0");

}

sb.append(hex);

}

return sb.toString().toLowerCase();

} }

Example 2: C# .NET の例

以下の例は、ファイルの SHA256 木構造ハッシュを計算する方法を示しています。この例は、ファイルの 場所を引数として指定して実行できます。

using System;

using System.IO;

using System.Security.Cryptography;

namespace ExampleTreeHash { class Program

{

static int ONE_MB = 1024 * 1024;

/**

* Compute the Hex representation of the SHA-256 tree hash for the * specified file

*

* @param args

ファイルの木構造ハッシュの計算

* args[0]: a file to compute a SHA-256 tree hash for */

public static void Main(string[] args) {

if (args.Length < 1) {

Console.WriteLine("Missing required filename argument");

Environment.Exit(-1);

}

FileStream inputFile = File.Open(args[0], FileMode.Open, FileAccess.Read);

try {

byte[] treeHash = ComputeSHA256TreeHash(inputFile);

Console.WriteLine("SHA-256 Tree Hash = {0}", BitConverter.ToString(treeHash).Replace("-", "").ToLower());

Console.ReadLine();

Environment.Exit(-1);

}

catch (IOException ioe) {

Console.WriteLine("Exception when reading from file {0}: {1}", inputFile, ioe.Message);

Console.ReadLine();

Environment.Exit(-1);

}

catch (Exception e) {

Console.WriteLine("Cannot locate MessageDigest algorithm for SHA-256: {0}", e.Message);

Console.WriteLine(e.GetType());

Console.ReadLine();

Environment.Exit(-1);

}

Console.ReadLine();

}

/**

* Computes the SHA-256 tree hash for the given file *

* @param inputFile

* A file to compute the SHA-256 tree hash for * @return a byte[] containing the SHA-256 tree hash */

public static byte[] ComputeSHA256TreeHash(FileStream inputFile) {

byte[][] chunkSHA256Hashes = GetChunkSHA256Hashes(inputFile);

return ComputeSHA256TreeHash(chunkSHA256Hashes);

}

/**

* Computes a SHA256 checksum for each 1 MB chunk of the input file. This * includes the checksum for the last chunk even if it is smaller than 1 MB.

*

* @param file

* A file to compute checksums on

* @return a byte[][] containing the checksums of each 1MB chunk */

public static byte[][] GetChunkSHA256Hashes(FileStream file) {

long numChunks = file.Length / ONE_MB;

if (file.Length % ONE_MB > 0) {

numChunks++;

}

ファイルの木構造ハッシュの計算

if (numChunks == 0) {

return new byte[][] { CalculateSHA256Hash(null, 0) };

}

byte[][] chunkSHA256Hashes = new byte[(int)numChunks][];

try {

byte[] buff = new byte[ONE_MB];

int bytesRead;

int idx = 0;

while ((bytesRead = file.Read(buff, 0, ONE_MB)) > 0) {

chunkSHA256Hashes[idx++] = CalculateSHA256Hash(buff, bytesRead);

}

return chunkSHA256Hashes;

} finally {

if (file != null) {

try {

file.Close();

}

catch (IOException ioe) {

throw ioe;

} } } } /**

* Computes the SHA-256 tree hash for the passed array of 1MB chunk * checksums.

*

* This method uses a pair of arrays to iteratively compute the tree hash * level by level. Each iteration takes two adjacent elements from the * previous level source array, computes the SHA-256 hash on their

* concatenated value and places the result in the next level's destination * array. At the end of an iteration, the destination array becomes the * source array for the next level.

*

* @param chunkSHA256Hashes

* An array of SHA-256 checksums

* @return A byte[] containing the SHA-256 tree hash for the input chunks */

public static byte[] ComputeSHA256TreeHash(byte[][] chunkSHA256Hashes) {

byte[][] prevLvlHashes = chunkSHA256Hashes;

while (prevLvlHashes.GetLength(0) > 1) {

int len = prevLvlHashes.GetLength(0) / 2;

if (prevLvlHashes.GetLength(0) % 2 != 0) {

len++;

}

byte[][] currLvlHashes = new byte[len][];

データをダウンロードするときのチェックサムの受信

int j = 0;

for (int i = 0; i < prevLvlHashes.GetLength(0); i = i + 2, j++) {

// If there are at least two elements remaining if (prevLvlHashes.GetLength(0) - i > 1)

{

// Calculate a digest of the concatenated nodes byte[] firstPart = prevLvlHashes[i];

byte[] secondPart = prevLvlHashes[i + 1];

byte[] concatenation = new byte[firstPart.Length + secondPart.Length];

System.Buffer.BlockCopy(firstPart, 0, concatenation, 0, firstPart.Length);

System.Buffer.BlockCopy(secondPart, 0, concatenation, firstPart.Length, secondPart.Length);

currLvlHashes[j] = CalculateSHA256Hash(concatenation, concatenation.Length);

} else

{ // Take care of remaining odd chunk currLvlHashes[j] = prevLvlHashes[i];

} }

prevLvlHashes = currLvlHashes;

}

return prevLvlHashes[0];

}

public static byte[] CalculateSHA256Hash(byte[] inputBytes, int count) {

SHA256 sha256 = System.Security.Cryptography.SHA256.Create();

byte[] hash = sha256.ComputeHash(inputBytes, 0, count);

return hash;

} } }

データをダウンロードするときのチェックサムの受信

ジョブの開始 API を使用してアーカイブを取得する場合は (「ジョブの開始 (ジョブの POST) (p. 273)」

を参照)、オプションでアーカイブの取得範囲を指定できます。同様に、ジョブの出力の取得 API を使用し てデータをダウンロードする場合は (「ジョブの出力の取得 (GET output) (p. 266)」を参照)、オプショ ンでダウンロードするデータの範囲を指定できます。これらの範囲には、アーカイブのデータを取得およ びダウンロードする際に理解していることが重要な 2 つの特性があります。取得する範囲は、アーカイブ に対してメガバイト単位に調整する必要があります。データをダウンロードしたときにチェックサム値を 受け取るには、取得する範囲とダウンロードする範囲が両方とも木構造ハッシュ可能である必要がありま す。この 2 つのタイプの範囲の調整は、次のように定義されています。

• メガバイト並列-A 範囲 [StartByte,EndBytes] は、メガバイト (1024*1024) のアラインメントされま す。StartBytesは 1 MB で割り切れ、EndBytes+ 1 は 1 MB で割り切れるか、指定したアーカイブの末尾 に等しい (アーカイブのバイトサイズから 1 を引いた値)。ジョブの開始 API で使用する範囲 (指定した 場合) は、メガバイト単位に調整する必要があります。

• 木構造ハッシュ可能 - 範囲 [StartBytes, EndBytes] は、その範囲で構築された木構造ハッシュのルート がアーカイブ全体の木構造ハッシュ内のノードに相当する場合のみ、アーカイブに対して木構造ハッ シュ可能です。ダウンロードしたデータのチェックサム値を受け取るには、取得する範囲とダウンロー

データをダウンロードするときのチェックサムの受信

ドする範囲が両方とも木構造ハッシュ可能である必要があります。範囲の例およびアーカイブ木構造 ハッシュとの関係については、「木構造ハッシュの例: 木構造ハッシュ可能なアーカイブの範囲を取得す る (p. 182)」を参照してください。

木構造ハッシュ可能な範囲は、メガバイト単位にも調整できることに注意してください。ただし、メガ バイト単位に調整された範囲が木構造ハッシュ可能であるとは限りません。

以下は、アーカイブデータをダウンロードしたときにチェックサムを受け取る場合を示しています。

• ジョブの開始リクエストで取得する範囲を指定せず、ジョブの取得リクエストでアーカイブ全体をダウ ンロードした場合。

• ジョブの開始リクエストで取得する範囲を指定せず、ジョブの取得リクエストでダウンロードする木構 造ハッシュ可能な範囲を指定した場合。

• ジョブの開始リクエストで取得する木構造ハッシュ可能な範囲を指定し、ジョブの取得リクエストでそ の範囲全体をダウンロードした場合。

• ジョブの開始リクエストで取得する木構造ハッシュ可能な範囲を指定し、ジョブの取得リクエストでダ ウンロードする木構造ハッシュ可能な範囲を指定した場合。

ジョブの開始リクエストで取得する範囲を指定し、その範囲が木構造ハッシュ可能ではない場合は、ジョ ブの取得リクエストでデータをダウンロードしたときにアーカイブデータを取得できますが、チェックサ ム値は返されません。

木構造ハッシュの例: 木構造ハッシュ可能なアーカイブの範囲を 取得する

ボールト内に 6.5 MB のアーカイブがあり、アーカイブの 2 MB 分を取得するとします。ジョブの開始リク エストで 2 MB の範囲を指定する方法によって、データのダウンロード時にデータチェックサム値を受け 取るかどうかが決定されます。次の図は、6.5 MB のアーカイブに対してダウンロードできる 2 つの 2 MB の範囲を示しています。両方ともメガバイト単位に調整されていますが、木構造ハッシュ可能な範囲は 1 つのみです。

木構造ハッシュ可能な範囲の指定

このセクションでは、木構造ハッシュ可能な範囲の正確な指定について説明します。木構造ハッシュ可能 な範囲は、アーカイブの一部分をダウンロードするときに、取得する範囲のデータと、取得したデータか

エラーレスポンス

らダウンロードする範囲を指定する場合に重要です。これらの範囲が両方とも木構造ハッシュ可能である 場合は、データをダウンロードしたときにチェックサムデータを受け取ります。

A 範囲 [A,B] はツリーハッシュ整列新しい木構造ハッシュが構築されている場合にのみ、アーカイブに関 して [A,B] の場合、その範囲の木構造ハッシュのルートは、アーカイブ全体の木構造ハッシュ内のノード に相当します。これについては、「木構造ハッシュの例: 木構造ハッシュ可能なアーカイブの範囲を取得す る (p. 182)」の図に示されています。このセクションでは、木構造ハッシュ可能な範囲の指定について説 明します。

[P, Q) を、N メガバイト (MB) のアーカイブの範囲クエリとします。P および Q は 1 MB の倍数です。実 際の包含範囲は [PMB,QMB — 1 バイト] ですが、単純化のために、[P,Q). これらの前提に立つと、次のよ うになります。

• もしPが奇数の場合、木構造ハッシュ可能な範囲は 1 つのみ、つまり [P,P+ 1 MB)。

• もしP偶数であり、k.は最大数です。ここで、P2 と書くことができます。k.*X、最大でk.木構造ハッシュ 可能な範囲P。X0 より大きい整数です。木構造ハッシュ可能な範囲は、次のカテゴリに含まれます。

• それぞれの i に対して、(0 <= i <= k) で、P + 2i < N の場合、[P, Q + 2i) は木構造ハッシュ可能な範囲 です。

• P = 0 は、A = 2[lgN]*0 である特殊なケースです。

エラーレスポンス

API は、エラーが発生すると以下の例外のいずれか 1 つを返します。

コード 説明 HTTP ステータ

スコード タイプ

AccessDeniedException AWS Identity and Access

Management (IAM) ポリシーによっ て許可されていないリソースにアク セスしようとした場合、または誤っ たAWSアカウント ID が要求 URI で 使用されました。詳細については、

「Amazon S3 Glacier での Identity and Access Management (p. 130)」

を参照してください。

403Forbidden クライ アント

BadRequest リクエストを処理することができな

い場合に返されます。 400 Bad

Request クライ

アント ExpiredTokenException リクエストで使用しているセキュリ

ティトークンが期限切れになってい る場合に返されます。

403Forbidden クライ アント

InsufficientCapacityException迅速リクエストを処理する容量が 足りない場合に返されます。この エラーは、迅速取り出しにのみ該当 し、標準取り出しまたは大容量取り 出しには該当しません。

503 Service

Unavailable サー バー

InvalidParameterValueExceptionリクエストのパラメータの指定が不

正である場合に返されます。 400 Bad

Request クライ

アント InvalidSignatureException リクエストの署名が無効である場合

に返されます。 403

Forbidden クライ アント

ドキュメント内 Amazon S3 Glacier - 開発者ガイド (ページ 181-200)