

# Scripting example for the Amazon S3 bucket lifecycle in the AWS CLI
<a name="cli-services-s3-lifecycle-example"></a>

このトピックでは、AWS Command Line Interface (AWS CLI) を使用した Amazon S3 バケットライフサイクルオペレーション用の bash スクリプトの例を使用します。このスクリプトの例では、一連の [https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html) コマンドを使用しています。シェルスクリプトは、コマンドラインインターフェイスで実行するように設計されたプログラムです。

**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)」および「」を参照してください。[Authentication and access credentials for the 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>

この例では、シェルスクリプトファイルの一連の関数を使用して、基本的な Amazon S3 オペレーションを操作する方法を示します。関数は、`bucket-operations.sh` という名前のシェルスクリプトファイルにあります。これらの関数を別のファイルで呼び出すことができます。各スクリプトファイルには、各関数について説明するコメントが含まれています。

各ステップの中間結果を表示するには、`-i` パラメータを指定してスクリプトを実行します。Amazon S3 コンソールを使用して、バケットまたはそのコンテンツの現在のステータスを表示できます。プロンプトで **Enter** キーを押すと、スクリプトは次のステップに進むだけです。

完全な例とダウンロード可能なスクリプトファイルについては、*GitHub* の *AWS コード例のリポジトリ*の「[Amazon S3 Bucket Lifecycle Operations](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**  
このメインスクリプトファイルは、別のファイルから取得できます。このファイルには、次のタスクを実行する関数が含まれています。  
+ バケットを作成し、それが存在することを検証する
+ ローカルコンピュータからバケットにファイルをコピーする
+ あるバケットの場所から別のバケットの場所にファイルをコピーする
+ バケットの内容の一覧表示
+ バケットからファイルを削除する
+ バケットの削除
`[bucket-operations.sh](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/aws-cli/bash-linux/s3/bucket-lifecycle-operations/bucket_operations.sh)` のコードを *GitHub* で表示します。

**test-bucket-operations.sh**  
シェルスクリプトファイル `test-bucket-operations.sh` は、`bucket-operations.sh` ファイルを取得し、各関数を呼び出すことによって関数を呼び出す方法を示しています。関数を呼び出すと、テストスクリプトは作成したすべてのリソースを削除します。  
`[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)` のコードを *GitHub* で表示します。

**awsdocs-general.sh**  
スクリプトファイル `awsdocs-general.sh` には、AWS CLI の高度なコード例で使用される汎用関数が格納されています。  
`[awsdocs-general.sh](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/aws-cli/bash-linux/s3/bucket-lifecycle-operations/awsdocs_general.sh)` のコードを *GitHub* で表示します。

## リファレンス
<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 ユーザーガイド](https://docs.aws.amazon.com//AmazonS3/latest/userguide/UsingBucket.html)」の「*Amazon S3 バケットの操作*」
+ 「[Amazon S3 ユーザーガイド](https://docs.aws.amazon.com//AmazonS3/latest/userguide/UsingObjects.html)」の「*Amazon S3 オブジェクトの操作*」
+ AWS SDK および AWS CLI のコード例を表示したり、提供したりするには、*GitHub* の [AWS コード例のリポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/)を参照してください。