

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

# 中的 Amazon S3 儲存貯體生命週期指令碼範例 AWS CLI
<a name="cli-services-s3-lifecycle-example"></a>

本主題使用 Bash 指令碼範例來說明使用 AWS Command Line Interface (AWS CLI) 的 Amazon S3 儲存貯體生命週期操作。此指令碼範例會使用 [https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html) 命令組。Shell 指令碼是專為在命令列界面中執行而設計的程式。

**Topics**
+ [開始之前](#cli-services-s3-lifecycle-example-before)
+ [關於此範例](#cli-services-s3-lifecycle-example-about)
+ [檔案](#cli-services-s3-lifecycle-example-files)
+ [參考](#cli-services-s3-lifecycle-example-references)

## 開始之前
<a name="cli-services-s3-lifecycle-example-before"></a>

在可以執行下列任何範例之前，您必須先完成下列事項。
+ 安裝及設定 AWS CLI。如需詳細資訊，請參閱[安裝或更新至最新版本的 AWS CLI](getting-started-install.md)及[的身分驗證和存取憑證 AWS CLI](cli-chap-authentication.md)。
+ 您使用的設定檔必須具有許可，以允許範例執行 AWS 的操作。
+  AWS 最佳實務是授予此程式碼的最低權限，或僅授予執行任務所需的許可。如需詳細資訊，請參閱*《IAM 使用者指南》*中的[授予最低權限](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)。
+ 此程式碼尚未在所有 AWS 區域中進行測試。某些 AWS 服務僅適用於特定 區域。如需詳細資訊，請參閱《AWS 一般參考指南》**中的[服務端點和配額](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)。
+ 執行此程式碼可能會導致 AWS 您的帳戶產生費用。您有責任確保在使用完該指令碼建立的所有資源後將這些資源移除。

Amazon S3 服務使用以下術語：
+ 儲存貯體 – 頂層 Amazon S3 資料夾。
+ 前綴 – 儲存貯體中的 Amazon S3 資料夾。
+ 物件 – 託管於 Amazon S3 儲存貯體中的任一個項目。

## 關於此範例
<a name="cli-services-s3-lifecycle-example-about"></a>

此範例示範如何使用 Shell 指令碼檔案中的一組函數與某些基本 Amazon S3 操作互動。這些函數位於名為 `bucket-operations.sh` 的 Shell 指令碼檔案中。您可以在另一個檔案中呼叫這些函數。每個指令碼檔案包含描述每個函數的註釋。

若要查看每個步驟的中繼結果，請執行具有 `-i` 參數的指令碼。您可以使用 Amazon S3 主控台檢視儲存貯體的目前狀態或其內容。在出現提示時按下 **ENTER** 後，指令碼才會繼續執行下一個步驟。

如需完整範例和可下載的指令碼檔案，請參閱 *GitHub* 上 *AWS 程式碼範例儲存庫*中的 [Amazon S3 儲存貯體生命週期操作](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/aws-cli/bash-linux/s3/bucket-lifecycle-operations)。

## 檔案
<a name="cli-services-s3-lifecycle-example-files"></a>

本範例內含下列檔案：

**bucket-operations.sh**  
此主指令碼檔案可從另一個檔案獲取。它包含執行以下任務的函數：  
+ 建立一個儲存貯體並驗證它是否存在
+ 將檔案從本機電腦複製至儲存貯體
+ 將檔案從一個儲存貯體位置複製到其他儲存貯體位置
+ 列出儲存貯體的內容
+ 從儲存貯體刪除檔案
+ 刪除儲存貯體
檢視 *GitHub* 上的 `[bucket-operations.sh](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/aws-cli/bash-linux/s3/bucket-lifecycle-operations/bucket_operations.sh)`。

**test-bucket-operations.sh**  
Shell 指令碼檔案 `test-bucket-operations.sh` 示範了如何透過獲取 `bucket-operations.sh` 檔案並呼叫每個函數來呼叫函數。呼叫函數後，測試指令碼會刪除它所建立的所有資源。  
檢視 *GitHub* 上的 `[test-bucket-operations.sh](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/aws-cli/bash-linux/s3/bucket-lifecycle-operations/test_bucket_operations.sh)`。

**awsdocs-general.sh**  
指令碼檔案 `awsdocs-general.sh` 會保存在各個 AWS CLI進階程式碼範例中使用的一般用途函數。  
檢視 *GitHub* 上的 `[awsdocs-general.sh](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/aws-cli/bash-linux/s3/bucket-lifecycle-operations/awsdocs_general.sh)`。

## 參考
<a name="cli-services-s3-lifecycle-example-references"></a>

**AWS CLI 參考：**
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/create-bucket.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/create-bucket.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/copy-object.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/copy-object.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-bucket.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-bucket.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-object.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-object.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/head-bucket.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/head-bucket.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/list-objects.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/list-objects.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html)

**其他參考：**
+ 《Amazon S3 使用者指南》**中的[使用 Amazon S3 儲存貯體](https://docs.aws.amazon.com//AmazonS3/latest/userguide/UsingBucket.html)。
+ 《Amazon S3 使用者指南》**中的[使用 Amazon S3 物件](https://docs.aws.amazon.com//AmazonS3/latest/userguide/UsingObjects.html)。
+ 若要檢視和貢獻 AWS SDK 和 AWS CLI 程式碼範例，請參閱 *GitHub* 上的[AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/)。