

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 中使用 Amazon Glacier AWS CLI
<a name="cli-services-glacier"></a>


| Amazon Glacier 簡介 | 
| --- | 
|    | 

本主題顯示執行 Amazon Glacier 常見任務的 AWS CLI 命令範例。這些範例示範如何使用 AWS CLI 將大型檔案分割成較小的部分，並從命令列上傳，以將大型檔案上傳至 Amazon Glacier。

您可以使用 AWS Command Line Interface () 存取 Amazon Glacier 功能AWS CLI。若要列出 Amazon Glacier 的 AWS CLI 命令，請使用下列命令。

```
aws glacier help
```

**注意**  
如需命令參考和其他範例，請參閱 *AWS CLI 命令參考*中的 `[aws glacier](https://docs.aws.amazon.com/cli/latest/reference/glacier/index.html)`。

**Topics**
+ [先決條件](#cli-services-glacier-prereqs)
+ [建立 Amazon Glacier 保存庫](#cli-services-glacier-vault)
+ [準備上傳檔案](#cli-services-glacier-prep)
+ [啟動分段上傳和上載檔案](#cli-services-glacier-initiate)
+ [完成上傳](#cli-services-glacier-complete)
+ [Resources](#cli-services-glacier-resources)

## 先決條件
<a name="cli-services-glacier-prereqs"></a>

若要執行 `glacier` 命令，您需要：
+ 安裝及設定 AWS CLI。如需詳細資訊，請參閱[安裝或更新至最新版本的 AWS CLI](getting-started-install.md)及[的身分驗證和存取憑證 AWS CLI](cli-chap-authentication.md)。
+ 本教學課程使用數個命令列工具，通常預先安裝在類 Unix 的操作系統上，包括 Linux 和 macOS。Windows 使用者可以通過安裝 [Cygwin](https://www.cygwin.com/) 並從 Cygwin 終端機執行命令來使用相同的工具。我們會備註在可用的情況下執行相同功能的 Windows 原生命令和公用程式。

## 建立 Amazon Glacier 保存庫
<a name="cli-services-glacier-vault"></a>

使用 `[create-vault](https://docs.aws.amazon.com/cli/latest/reference/glacier/create-vault.html)` 命令建立一個保存庫。

```
$ aws glacier create-vault --account-id - --vault-name myvault
{
    "location": "/123456789012/vaults/myvault"
}
```

**注意**  
所有 Amazon Glacier 命令都需要帳戶 ID 參數。使用連字號字元 (`--account-id -`) 來使用目前的帳戶。

## 準備上傳檔案
<a name="cli-services-glacier-prep"></a>

為測試上載建立一個檔案。以下命令將建立名為 *largefile* 的檔案，其中包含 3 MiB 的隨機數據。

**Linux 或 macOS**

```
$ dd if=/dev/urandom of=largefile bs=3145728 count=1
1+0 records in
1+0 records out
3145728 bytes (3.1 MB) copied, 0.205813 s, 15.3 MB/s
```

`dd` 是一個實用程式，可將輸入檔案中的大量位元組複製到輸出檔案。上述範例使用系統裝置檔案 `/dev/urandom` 作為隨機資料的來源。`fsutil` 在 Windows 執行類似的功能。

**Windows**

```
C:\> fsutil file createnew largefile 3145728
File C:\temp\largefile is created
```

接下來，使用檔案分割器將檔案分成 1 MiB (1,048,576 位元組) 區塊。

```
$ split -b 1048576 --verbose largefile chunk
creating file `chunkaa'
creating file `chunkab'
creating file `chunkac'
```

## 啟動分段上傳和上載檔案
<a name="cli-services-glacier-initiate"></a>

使用 `[initiate-multipart-upload](https://docs.aws.amazon.com/cli/latest/reference/glacier/initiate-multipart-upload.html)`命令在 Amazon Glacier 中建立分段上傳。

```
$ aws glacier initiate-multipart-upload --account-id - --archive-description "multipart upload test" --part-size 1048576 --vault-name myvault
{
    "uploadId": "19gaRezEXAMPLES6Ry5YYdqthHOC_kGRCT03L9yetr220UmPtBYKk-OssZtLqyFu7sY1_lR7vgFuJV6NtcV5zpsJ",
    "location": "/123456789012/vaults/myvault/multipart-uploads/19gaRezEXAMPLES6Ry5YYdqthHOC_kGRCT03L9yetr220UmPtBYKk-OssZtLqyFu7sY1_lR7vgFuJV6NtcV5zpsJ"
}
```

Amazon Glacier 需要以位元組為單位的每個部分大小 （此範例中為 1 MiB)、您的保存庫名稱，以及帳戶 ID 來設定分段上傳。操作完成時， 會 AWS CLI 輸出上傳 ID。將上傳 ID 儲存到 Shell 變數以供稍後使用。

**Linux 或 macOS**

```
$ UPLOADID="19gaRezEXAMPLES6Ry5YYdqthHOC_kGRCT03L9yetr220UmPtBYKk-OssZtLqyFu7sY1_lR7vgFuJV6NtcV5zpsJ"
```

**Windows**

```
C:\> set UPLOADID="19gaRezEXAMPLES6Ry5YYdqthHOC_kGRCT03L9yetr220UmPtBYKk-OssZtLqyFu7sY1_lR7vgFuJV6NtcV5zpsJ"
```

接下來，使用 `[upload-multipart-part](https://docs.aws.amazon.com/cli/latest/reference/glacier/upload-multipart-part.html)` 命令來上傳這三個部分。

```
$ aws glacier upload-multipart-part --upload-id $UPLOADID --body chunkaa --range 'bytes 0-1048575/*' --account-id - --vault-name myvault
{
    "checksum": "e1f2a7cd6e047fa606fe2f0280350f69b9f8cfa602097a9a026360a7edc1f553"
}
$ aws glacier upload-multipart-part --upload-id $UPLOADID --body chunkab --range 'bytes 1048576-2097151/*' --account-id - --vault-name myvault
{
    "checksum": "e1f2a7cd6e047fa606fe2f0280350f69b9f8cfa602097a9a026360a7edc1f553"
}
$ aws glacier upload-multipart-part --upload-id $UPLOADID --body chunkac --range 'bytes 2097152-3145727/*' --account-id - --vault-name myvault
{
    "checksum": "e1f2a7cd6e047fa606fe2f0280350f69b9f8cfa602097a9a026360a7edc1f553"
}
```

**注意**  
上面的範例使用貨幣符號 (`$`) 來參考 Linux 上的 `UPLOADID` Shell 變數內容。在 Windows 命令列，請在變數名稱 (例如 `%UPLOADID%`) 任一側使用百分比符號 (%)。

您必須在上傳時指定每個部分的位元組範圍，以便 Amazon Glacier 可以正確順序重新組合。每一部分是 1048576 bytes，所以第一部分的位元組是 0-1048575，第二部分是 1048576-2097151，第三部分是 2097152-3145727。

## 完成上傳
<a name="cli-services-glacier-complete"></a>

Amazon Glacier 需要原始檔案的樹雜湊，以確認所有上傳的片段都完 AWS 好無損。

若要計算樹雜湊值，您必須將檔案拆分為 1 MiB 部分，並計算每個部分的二進位 SHA-256 雜湊值。然後，將雜湊值清單分為幾對，在每一對中合併兩個二進位雜湊值，並取結果的雜湊值。重複此過程，直到只剩下一個雜湊值。如果在任何階層都有奇數的雜湊值，則將其推進到下一階層而不進行修改。

使用命令列公用程式正確計算樹雜湊函數的關鍵是將每個雜湊值儲存為二進位格式，並且只在最後一步轉換為十六進位。組合或雜湊樹中任何雜湊的十六進位版本將導致錯誤的結果。

**注意**  
Windows 使用者可以使用 `type` 命令代替`cat`。位於 [OpenSSL.org](https://www.openssl.org/source/)的 OpenSSL 適用於 Windows。

**計算樹雜湊函數**

1. 如果尚未這麼做，請將原始檔案拆分為 1 MiB 部分。

   ```
   $ split --bytes=1048576 --verbose largefile chunk
   creating file `chunkaa'
   creating file `chunkab'
   creating file `chunkac'
   ```

1. 計算並存放每個區塊的二進位 SHA-256 雜湊。

   ```
   $ openssl dgst -sha256 -binary chunkaa > hash1
   $ openssl dgst -sha256 -binary chunkab > hash2
   $ openssl dgst -sha256 -binary chunkac > hash3
   ```

1. 合併前兩個雜湊，並取結果的二進位雜湊值。

   ```
   $ cat hash1 hash2 > hash12
   $ openssl dgst -sha256 -binary hash12 > hash12hash
   ```

1. 以與區塊 `ac` 的雜湊值和雜湊結果將區塊 `aa` 和 `ab` 的父系雜湊值作結合，此時會輸出十六進位。將結果存放在 Shell 變數中。

   ```
   $ cat hash12hash hash3 > hash123
   $ openssl dgst -sha256 hash123
   SHA256(hash123)= 9628195fcdbcbbe76cdde932d4646fa7de5f219fb39823836d81f0cc0e18aa67
   $ TREEHASH=9628195fcdbcbbe76cdde932d4646fa7de5f219fb39823836d81f0cc0e18aa67
   ```

最後，用 `[complete-multipart-upload](https://docs.aws.amazon.com/cli/latest/reference/glacier/complete-multipart-upload.html)` 命令完成上傳。此命令採用原始檔案的大小 (以位元組為單位)、最終的樹雜湊值 (十六進位) 以及您的帳戶 ID 和保存庫名稱。

```
$ aws glacier complete-multipart-upload --checksum $TREEHASH --archive-size 3145728 --upload-id $UPLOADID --account-id - --vault-name myvault
{
    "archiveId": "d3AbWhE0YE1m6f_fI1jPG82F8xzbMEEZmrAlLGAAONJAzo5QdP-N83MKqd96Unspoa5H5lItWX-sK8-QS0ZhwsyGiu9-R-kwWUyS1dSBlmgPPWkEbeFfqDSav053rU7FvVLHfRc6hg",
    "checksum": "9628195fcdbcbbe76cdde932d4646fa7de5f219fb39823836d81f0cc0e18aa67",
    "location": "/123456789012/vaults/myvault/archives/d3AbWhE0YE1m6f_fI1jPG82F8xzbMEEZmrAlLGAAONJAzo5QdP-N83MKqd96Unspoa5H5lItWX-sK8-QS0ZhwsyGiu9-R-kwWUyS1dSBlmgPPWkEbeFfqDSav053rU7FvVLHfRc6hg"
}
```

您還可以使用 `[describe-vault](https://docs.aws.amazon.com/cli/latest/reference/glacier/describe-vault.html)` 命令來檢查保存庫的狀態。

```
$ aws glacier describe-vault --account-id - --vault-name myvault
{
    "SizeInBytes": 3178496,
    "VaultARN": "arn:aws:glacier:us-west-2:123456789012:vaults/myvault",
    "LastInventoryDate": "2018-12-07T00:26:19.028Z",
    "NumberOfArchives": 1,
    "CreationDate": "2018-12-06T21:23:45.708Z",
    "VaultName": "myvault"
}
```

**注意**  
每天更新一次保存庫狀態。請參閱[使用保存庫](https://docs.aws.amazon.com/amazonglacier/latest/dev/working-with-vaults.html)了解更多資訊。

現在可以安全地移除您先前建立的區塊和雜湊檔案。

```
$ rm chunk* hash*
```

如需分段上傳的詳細資訊，請參閱《*Amazon Glacier 開發人員指南*》[中的在分段和運算檢查中上傳大型封存](https://docs.aws.amazon.com/amazonglacier/latest/dev/uploading-archive-mpu.html)。 [https://docs.aws.amazon.com/amazonglacier/latest/dev/checksum-calculations.html](https://docs.aws.amazon.com/amazonglacier/latest/dev/checksum-calculations.html)

## Resources
<a name="cli-services-glacier-resources"></a>

**AWS CLI 參考：**
+ `[aws glacier](https://docs.aws.amazon.com/cli/latest/reference/glacier/index.html)`
+ `[aws glacier complete-multipart-upload](https://docs.aws.amazon.com/cli/latest/reference/glacier/complete-multipart-upload.html)`
+ `[aws glacier create-vault](https://docs.aws.amazon.com/cli/latest/reference/glacier/create-vault.html)`
+ `[aws glacier describe-vault](https://docs.aws.amazon.com/cli/latest/reference/glacier/describe-vault.html)`
+ `[aws glacier initiate-multipart-upload](https://docs.aws.amazon.com/cli/latest/reference/glacier/initiate-multipart-upload.html)`

**服務參考：**
+ [Amazon Glacier 開發人員指南](https://docs.aws.amazon.com/amazonglacier/latest/dev/)
+ 《*Amazon Glacier 開發人員指南*》中的[分段上傳大型封存](https://docs.aws.amazon.com/amazonglacier/latest/dev/uploading-archive-mpu.html) 
+ 《*Amazon Glacier 開發人員指南*》中的[運算檢查總和](https://docs.aws.amazon.com/amazonglacier/latest/dev/checksum-calculations.html) 
+ 《*Amazon Glacier 開發人員指南*》中的[使用保存庫](https://docs.aws.amazon.com/amazonglacier/latest/dev/working-with-vaults.html) 