

# AWS CLI での Amazon S3 の使用
<a name="cli-services-s3"></a>


| Amazon Simple Storage Service (Amazon S3) のご紹介 | 
| --- | 
|    | 

AWS Command Line Interface (AWS CLI) を使用して、Amazon Simple Storage Service (Amazon S3) の機能にアクセスできます。Amazon S3 は、高度にスケーラブルで耐久性のあるオブジェクトストレージサービスです。Amazon S3 は実質的に無制限のストレージ容量を提供するように設計されているため、幅広いデータストレージと管理のニーズに最適なソリューションです。

Amazon S3 では、小さなファイルから大きなデータセットまで、任意の量のデータをオブジェクトの形式で保存および取得できます。各オブジェクトはバケットと呼ばれるコンテナに保存されます。これは、AWS マネジメントコンソールを通じて、または AWS SDK、ツール、および AWS CLI を使ってプログラム的にアクセスおよび管理できます。

基本的なストレージを含む Amazon S3 には、ライフサイクル管理、バージョニング、スケーラビリティ、セキュリティなどのさまざまな機能も搭載されています。これらは他の AWS のサービスと統合されているため、ニーズに合わせて拡張できるクラウドベースのソリューションを構築できます。

AWS CLI は、Amazon S3 へのアクセスのために 2 つの階層のコマンドを提供します。
+ **s3** – オブジェクトおよびバケットの作成、操作、削除、および同期など、一般的なタスクの実行を簡素化する AWS CLI のためのカスタムの高レベルコマンドです。
+ **s3api**— Amazon S3 API すべてのオペレーションへの直接アクセスを公開することで、高度なオペレーションを実行できます。

**Topics**
+ [Using high-level (s3) commands in the AWS CLI](cli-services-s3-commands.md)
+ [Using API-Level (s3api) commands in the AWS CLI](cli-services-s3-apicommands.md)
+ [Scripting example for the Amazon S3 bucket lifecycle in the AWS CLI](cli-services-s3-lifecycle-example.md)

# Using high-level (s3) commands in the AWS CLI
<a name="cli-services-s3-commands"></a>

このトピックでは、AWS CLI で [https://docs.aws.amazon.com/cli/latest/reference/s3/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3/index.html) コマンドを使用して、Amazon S3 のバケットとオブジェクトを管理するために使用できるコマンドの一部について説明します。このトピックで説明されていないコマンドおよびその他のコマンドの例については、「*AWS CLI リファレンス*」「[https://docs.aws.amazon.com/cli/latest/reference/s3/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3/index.html)」コマンドを参照してください。

高レベル `aws s3` コマンドを使用すると、Amazon S3 オブジェクトの管理が容易になります。これらのコマンドにより、Amazon S3 のコンテンツの管理をそれ自体の内部およびローカルディレクトリ内で管理することができます。

**Topics**
+ [前提条件](#using-s3-commands-prereqs)
+ [開始する前に](#using-s3-commands-before)
+ [バケットを作成する](#using-s3-commands-managing-buckets-creating)
+ [バケットとオブジェクトの一覧表示](#using-s3-commands-listing-buckets)
+ [バケットの削除](#using-s3-commands-delete-buckets)
+ [オブジェクトの削除](#using-s3-commands-delete-objects)
+ [オブジェクトの移動](#using-s3-commands-managing-objects-move)
+ [オブジェクトのコピー](#using-s3-commands-managing-objects-copy)
+ [オブジェクトの同期](#using-s3-commands-managing-objects-sync)
+ [s3 コマンドでよく使用されるオプション](#using-s3-commands-managing-objects-param)
+ [リソース](#using-s3-commands-managing-buckets-references)

## 前提条件
<a name="using-s3-commands-prereqs"></a>

`s3` コマンドを実行するには、次のことが必要です。
+ AWS CLI をインストールして設定します。詳細については、「[AWS CLI の最新バージョンのインストールまたは更新](getting-started-install.md)」および「」を参照してください。[Authentication and access credentials for the AWS CLI](cli-chap-authentication.md)
+ 使用するプロファイルには、例によって実行される AWS オペレーションに対する許可が必要です。
+ Amazon S3 の以下の用語を解説します。
  + **バケット** - トップレベルの Amazon S3 フォルダ。
  + **プレフィックス** - バケット内の Amazon S3 フォルダ。
  + **オブジェクト** - Amazon S3 バケットでホストされているすべてのアイテム。

## 開始する前に
<a name="using-s3-commands-before"></a>

このセクションでは、`aws s3` コマンドを使用する前の注意事項をいくつか示します。

### 大容量オブジェクトのアップロード
<a name="using-s3-commands-before-large"></a>

`aws s3` コマンドを使用して大きなオブジェクトを Amazon S3 バケットにアップロードする場合、AWS CLI は自動的にマルチパートアップロードを実行します。これらの `aws s3` コマンドを使用した場合、失敗したアップロードを再開することはできません。

マルチパートアップロードがタイムアウトして失敗した場合、または AWS CLI で手動でキャンセルした場合、AWS CLI はアップロードを停止し、作成されたすべてのファイルをクリーンアップします。この処理には数分かかることもあります。

マルチパートアップロードまたはクリーンアッププロセスが kill コマンドまたはシステム障害によってキャンセルされた場合、作成されたファイルは Amazon S3 バケットに残ります。マルチパートアップロードをクリーンアップするには、[s3api abort-multipart-upload](https://docs.aws.amazon.com/cli/latest/reference/s3api/abort-multipart-upload.html) コマンドを使用します。

### マルチパートコピーのファイルプロパティとタグ
<a name="using-s3-commands-before-tags"></a>

1 つの Amazon S3 バケットの場所から別の Amazon S3 バケットの場所にファイルをコピーするために `aws s3` 名前空間で AWS CLI バージョン 1 のコマンドバージョンを使用しており、そのオペレーションが[マルチパートコピー](https://docs.aws.amazon.com/AmazonS3/latest/userguide/CopyingObjctsMPUapi.html)を使用する場合は、コピー先にソースオブジェクトのファイルプロパティがコピーされません。

デフォルトで、マルチパートコピーを実行する `s3` 名前空間の AWS CLI バージョン 2 のコマンドは、ソースのすべてのタグと、プロパティのセット (`content-type`、`content-language`、`content-encoding`、`content-disposition`、`cache-control`、`expires`) をコピー先に転送します。

これにより、AWS CLI バージョン 1 を使用する場合には行われなかった追加の AWS API コールが Amazon S3 エンドポイントに対して実行されることになる場合があります。これには、`HeadObject`、`GetObjectTagging`、および `PutObjectTagging` が含まれます。

AWS CLI バージョン 2 コマンドでこのデフォルト動作を変更する必要がある場合は、`--copy-props` パラメータを使用して次のオプションのいずれかを指定します。
+ **default ** - デフォルト値。コピーに、ソースオブジェクトにアタッチされたすべてのタグと、マルチパートコピー以外に使用される `--metadata-directive` パラメータの次のプロパティが含まれることを指定します。`content-type`、`content-language`、`content-encoding`、`content-disposition`、`cache-control`, `expires`、`metadata`。
+ **metadata-directive ** - `--metadata-directive` マルチパートコピー以外に使用されるパラメータによって包含されるプロパティのみがコピーに含まれることを指定します。タグはコピーされません。
+ **none** - コピーにソースオブジェクトのプロパティを含みません。

## バケットを作成する
<a name="using-s3-commands-managing-buckets-creating"></a>

[https://docs.aws.amazon.com/cli/latest/reference/s3/mb.html](https://docs.aws.amazon.com/cli/latest/reference/s3/mb.html) コマンドを使用して、バケットを作成します。バケット名は***グローバル***に一意で (Amazon S3 全体で一意)、DNS に準拠している必要があります。

バケット名には、小文字の英文字、数字、ハイフン、およびピリオドを使用することができます。バケット名の先頭と末尾は文字または数値とし、ハイフンまたは別のピリオドの横にピリオドを使用することはできません。

**構文**

```
$ aws s3 mb <target> [--options]
```

### s3 mb の例
<a name="using-s3-commands-managing-buckets-creating-examples"></a>

次の例では、`s3://amzn-s3-demo-bucket` バケットを作成します。

```
$ aws s3 mb s3://amzn-s3-demo-bucket
```

## バケットとオブジェクトの一覧表示
<a name="using-s3-commands-listing-buckets"></a>

バケット、フォルダ、オブジェクトのいずれかを一覧表示するには、[https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html](https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html) コマンドを使用します。ターゲットまたはオプションを指定せずにコマンドを使用すると、すべてのバケットが一覧表示されます。

**構文**

```
$ aws s3 ls <target> [--options]
```

このコマンドで使用するいくつかの一般的なオプションと例については、「[s3 コマンドでよく使用されるオプション](#using-s3-commands-managing-objects-param)」を参照してください。使用可能なオプションの完全なリストについては、「*AWS CLI コマンドリファレンス*」の「[https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html](https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html)」を参照してください。

### s3 ls の例
<a name="using-s3-commands-managing-objects-list-examples"></a>

次の例では、すべての Amazon S3 バケットを一覧表示します。

```
$ aws s3 ls
2018-12-11 17:08:50 amzn-s3-demo-bucket1
2018-12-14 14:55:44 amzn-s3-demo-bucket2
```

次のコマンドでは、バケット内のすべてのオブジェクトとプレフィックスを一覧表示します。この出力例では、プレフィックス `example/` 内に `MyFile1.txt` という名前のファイル が 1 つあります。

```
$ aws s3 ls s3://amzn-s3-demo-bucket
                           PRE example/
2018-12-04 19:05:48          3 MyFile1.txt
```

コマンドに含めることによって、出力を特定のプレフィックスに絞り込むことができます。次のコマンドでは、*bucket-name/example/* 内のオブジェクトを一覧表示します (つまり、プレフィックス *example/* でフィルタリングされた、*bucket-name* 内のオブジェクトを表示します)。

```
$ aws s3 ls s3://amzn-s3-demo-bucket/example/
2018-12-06 18:59:32          3 MyFile1.txt
```

特定のリージョンのバケットとオブジェクトのみを表示するには、`--region` オプションを使用します。

```
$ aws s3 ls --region us-east-2
2018-12-06 18:59:32          3 MyFile1.txt
```

バケットとオブジェクトのリストが大きい場合は、`--max-items` または `--page-size` オプションを使用して結果をページ分割できます。`--max-items` オプションは 1 回の呼び出しで返されるバケットとオブジェクトの合計数を制限し、`--page-size` オプションはそのうちの 1 ページに表示される数を制限します。

```
$ aws s3 ls --max-items 100 --page-size 10
```

ページ分割の詳細については、「[--page-size パラメータの使用方法](cli-usage-pagination.md#cli-usage-pagination-pagesize)」および「[--max-items パラメータの使用方法](cli-usage-pagination.md#cli-usage-pagination-maxitems)」を参照してください。

## バケットの削除
<a name="using-s3-commands-delete-buckets"></a>

バケットを削除するには、[https://docs.aws.amazon.com/cli/latest/reference/s3/rb.html](https://docs.aws.amazon.com/cli/latest/reference/s3/rb.html) コマンドを使用します。

**構文**

```
$ aws s3 rb <target> [--options]
```

### s3 rb の例
<a name="using-s3-commands-removing-buckets-examples"></a>

次の例では、`s3://amzn-s3-demo-bucket` バケットを削除します。

```
$ aws s3 rb s3://amzn-s3-demo-bucket
```

デフォルトでは、オペレーションが成功するにはバケットが空である必要があります。空でないバケットを削除するには、`--force` オプションを含める必要があります。以前に削除されたが保持されているオブジェクトを含む、バージョニングされたバケットを使用している場合、このコマンドでバケットを削除することは*できません*。すべての内容を削除しておく必要があります。

次の例では、バケット内のすべてのオブジェクトとプレフィックスを削除し、さらにバケット自体を削除します。

```
$ aws s3 rb s3://amzn-s3-demo-bucket --force
```

## オブジェクトの削除
<a name="using-s3-commands-delete-objects"></a>

バケット内またはローカルディレクトリ内のオブジェクトを削除するには、[https://docs.aws.amazon.com/cli/latest/reference/s3/rm.html](https://docs.aws.amazon.com/cli/latest/reference/s3/rm.html) コマンドを使用します。

**構文**

```
$ aws s3 rm  <target> [--options]
```

このコマンドで使用するいくつかの一般的なオプションと例については、「[s3 コマンドでよく使用されるオプション](#using-s3-commands-managing-objects-param)」を参照してください。オプションの完全なリストについては、「*AWS CLI コマンドリファレンス*」の「[https://docs.aws.amazon.com/cli/latest/reference/s3/rm.html](https://docs.aws.amazon.com/cli/latest/reference/s3/rm.html)」を参照してください。

### s3 rm の例
<a name="using-s3-commands-delete-objects-examples"></a>

以下の例では、`filename.txt` から `s3://amzn-s3-demo-bucket/example` が削除されます。

```
$ aws s3 rm s3://amzn-s3-demo-bucket/example/filename.txt
```

以下の例では、`s3://amzn-s3-demo-bucket/example` オプションを使用して `--recursive` からすべてのオブジェクトが削除されます。

```
$ aws s3 rm s3://amzn-s3-demo-bucket/example --recursive
```

## オブジェクトの移動
<a name="using-s3-commands-managing-objects-move"></a>

バケットまたはローカルディレクトリからオブジェクトを移動するには、[https://docs.aws.amazon.com/cli/latest/reference/s3/mv.html](https://docs.aws.amazon.com/cli/latest/reference/s3/mv.html) コマンドを使用します。`s3 mv` コマンドを使用すると、ソースオブジェクトまたはファイルが指定された宛先にコピーされ、ソースオブジェクトまたはファイルが削除されます。

**構文**

```
$ aws s3 mv <source> <target> [--options]
```

このコマンドで使用するいくつかの一般的なオプションと例については、「[s3 コマンドでよく使用されるオプション](#using-s3-commands-managing-objects-param)」を参照してください。使用可能なオプションの完全なリストについては、「*AWS CLI コマンドリファレンス*」の「[https://docs.aws.amazon.com/cli/latest/reference/s3/mv.html](https://docs.aws.amazon.com/cli/latest/reference/s3/mv.html)」を参照してください。

**警告**  
Amazon S3 ソースまたは宛先 URI で任意のタイプのアクセスポイント ARN またはアクセスポイントエイリアスを使用している場合、ソースと宛先 Amazon S3 URI が異なる基盤となるバケットに対して解決されるように特に注意する必要があります。ソースバケットと宛先バケットが同じ場合、ソースファイルまたはオブジェクトをそれ自体に移動する可能性があるため、ソースファイルまたはオブジェクトが誤って削除される恐れがあります。ソースバケットと宛先バケットが同じでないことを確認するには、`--validate-same-s3-paths` パラメータを使用するか、環境変数 ``AWS_CLI_S3_MV_VALIDATE_SAME_S3_PATHS`` を `true` に設定します。

### s3 mv の例
<a name="using-s3-commands-managing-objects-move-examples"></a>

次の例では、すべてのオブジェクトを `s3://amzn-s3-demo-bucket/example` から `s3://amzn-s3-demo-bucket/` に移動します。

```
$ aws s3 mv s3://amzn-s3-demo-bucket/example s3://amzn-s3-demo-bucket/
```

次の例では、`s3 mv` コマンドを使用して、ローカルファイルを現在の作業ディレクトリから Amazon S3 バケットに移動します。

```
$ aws s3 mv filename.txt s3://amzn-s3-demo-bucket
```

次の例では、ファイルを Amazon S3 バケットから現在の作業ディレクトリに移動します。`./` では、現在の作業ディレクトリを指定します。

```
$ aws s3 mv s3://amzn-s3-demo-bucket/filename.txt ./
```

## オブジェクトのコピー
<a name="using-s3-commands-managing-objects-copy"></a>

バケットまたはローカルディレクトリからオブジェクトをコピーするには、[https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html) コマンドを使用します。

**構文**

```
$ aws s3 cp <source> <target> [--options]
```

標準入力 (`stdin`) または標準出力 (`stdout`) へのファイルストリーミングには dash パラメータを使用できます。

**警告**  
PowerShellを使用している場合、シェルによって CRLF のエンコーディングが変更されたり、パイプされた入力/出力またはリダイレクトされた出力に CRLF が追加されたりすることがあります。

`s3 cp` コマンドでは、次の構文を使用して、`stdin` から指定先のバケットにファイルストリームをアップロードします。

**構文**

```
$ aws s3 cp - <target> [--options]
```

`s3 cp` コマンドは、次の構文を使用して `stdout` 用の Amazon S3 ファイルストリームをダウンロードします。

**構文**

```
$ aws s3 cp <target> [--options] -
```

このコマンドで使用するいくつかの一般的なオプションと例については、「[s3 コマンドでよく使用されるオプション](#using-s3-commands-managing-objects-param)」を参照してください。オプションの完全なリストについては、「*AWS CLI コマンドリファレンス*」の「[https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html)」を参照してください。

### `s3 cp` の例
<a name="using-s3-commands-managing-objects-copy-examples"></a>

次の例では、すべてのオブジェクトを `s3://amzn-s3-demo-bucket/example` から `s3://amzn-s3-demo-bucket/` にコピーします。

```
$ aws s3 cp s3://amzn-s3-demo-bucket/example s3://amzn-s3-demo-bucket/
```

次の例では、`s3 cp` コマンドを使用して、現在の作業ディレクトリから Amazon S3 バケットにローカルファイルをコピーします。

```
$ aws s3 cp filename.txt s3://amzn-s3-demo-bucket
```

次の例では、ファイルを Amazon S3 バケットから現在の作業ディレクトリにコピーします (`./` では現在の作業ディレクトリを指定します)。

```
$ aws s3 cp s3://amzn-s3-demo-bucket/filename.txt ./
```

次の例では、echo を使用して「hello world」というテキストを `s3://bucket-name/filename.txt` ファイルにストリームします。

```
$ echo "hello world" | aws s3 cp - s3://amzn-s3-demo-bucket/filename.txt
```

次の例では、`s3://amzn-s3-demo-bucket/filename.txt` ファイルを `stdout` にストリーミングし、その内容をコンソールに出力します。

```
$ aws s3 cp s3://amzn-s3-demo-bucket/filename.txt -
hello world
```

次の例では、`s3://bucket-name/pre` の内容を `stdout` にストリーミングし、`bzip2` コマンドを使用してファイルを圧縮します。この圧縮したファイルを `key.bz2` という名前で `s3://bucket-name` にアップロードします。

```
$ aws s3 cp s3://amzn-s3-demo-bucket/pre - | bzip2 --best | aws s3 cp - s3://amzn-s3-demo-bucket/key.bz2
```

## オブジェクトの同期
<a name="using-s3-commands-managing-objects-sync"></a>

[https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html](https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html) コマンドはバケットとディレクトリの内容、または 2 つのバケットの内容を同期します。通常、`s3 sync` は欠落しているか古くなったファイルやオブジェクトを、ソースとターゲットの間でコピーします。ただし、`--delete` オプションを指定して、ソースに存在しないファイルまたはオブジェクトをターゲットから削除することもできます。

**構文**

```
$ aws s3 sync <source> <target> [--options]
```

このコマンドで使用するいくつかの一般的なオプションと例については、「[s3 コマンドでよく使用されるオプション](#using-s3-commands-managing-objects-param)」を参照してください。オプションの完全なリストについては、「*AWS CLI コマンドリファレンス*」の「[https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html](https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html)」を参照してください。

### s3 sync の例
<a name="using-s3-commands-managing-objects-sync-examples"></a>

次の例では、*amzn-s3-demo-bucket* という名前のバケット内の *path* という名前の Amazon S3 プレフィックスの内容を現在の作業ディレクトリと同期します。

`s3 sync` は、同期先で同じ名前を持つファイルと、サイズや変更時間が異なるファイルを更新します。出力には、同期中に実行された特定のオペレーションが表示されます。このオペレーションでは、サブディレクトリ `MySubdirectory` とその内容を `s3://amzn-s3-demo-bucket/path/MySubdirectory` と再帰的に同期することに注意してください。

```
$ aws s3 sync . s3://amzn-s3-demo-bucket/path
upload: MySubdirectory\MyFile3.txt to s3://amzn-s3-demo-bucket/path/MySubdirectory/MyFile3.txt
upload: MyFile2.txt to s3://amzn-s3-demo-bucket/path/MyFile2.txt
upload: MyFile1.txt to s3://amzn-s3-demo-bucket/path/MyFile1.txt
```

次の例 (前の例の拡張) では、`--delete` オプションの使用方法を示します。

```
// Delete local file
$ rm ./MyFile1.txt

// Attempt sync without --delete option - nothing happens
$ aws s3 sync . s3://amzn-s3-demo-bucket/path

// Sync with deletion - object is deleted from bucket
$ aws s3 sync . s3://amzn-s3-demo-bucket/path --delete
delete: s3://amzn-s3-demo-bucket/path/MyFile1.txt

// Delete object from bucket
$ aws s3 rm s3://amzn-s3-demo-bucket/path/MySubdirectory/MyFile3.txt
delete: s3://amzn-s3-demo-bucket/path/MySubdirectory/MyFile3.txt

// Sync with deletion - local file is deleted
$ aws s3 sync s3://amzn-s3-demo-bucket/path . --delete
delete: MySubdirectory\MyFile3.txt

// Sync with Infrequent Access storage class
$ aws s3 sync . s3://amzn-s3-demo-bucket/path --storage-class STANDARD_IA
```

`--delete` オプションを使用する場合、`--exclude` オプションと `--include` オプションによって、`s3 sync` オペレーション中に削除するファイルやオブジェクトをフィルタリングできます。この場合、パラメータ文字列で、ターゲットディレクトリまたはバケットに関連して、削除から除外するか、削除に含めるファイルを指定する必要があります。例を以下に示します。

```
Assume local directory and s3://amzn-s3-demo-bucket/path currently in sync and each contains 3 files:
MyFile1.txt
MyFile2.rtf
MyFile88.txt
'''

// Sync with delete, excluding files that match a pattern. MyFile88.txt is deleted, while remote MyFile1.txt is not.
$ aws s3 sync . s3://amzn-s3-demo-bucket/path --delete --exclude "path/MyFile?.txt"
delete: s3://amzn-s3-demo-bucket/path/MyFile88.txt
'''

// Sync with delete, excluding MyFile2.rtf - local file is NOT deleted
$ aws s3 sync s3://amzn-s3-demo-bucket/path . --delete --exclude "./MyFile2.rtf"
download: s3://amzn-s3-demo-bucket/path/MyFile1.txt to MyFile1.txt
'''

// Sync with delete, local copy of MyFile2.rtf is deleted
$ aws s3 sync s3://amzn-s3-demo-bucket/path . --delete
delete: MyFile2.rtf
```

## s3 コマンドでよく使用されるオプション
<a name="using-s3-commands-managing-objects-param"></a>

このトピックで説明するコマンドでは、次のオプションがよく使用されます。コマンドで使用できるオプションの完全なリストについては、「[AWS CLI バージョン 2 リファレンスガイド](https://docs.aws.amazon.com/cli/latest/reference/index.html)」で特定のコマンドを参照してください。

**acl**  
`s3 sync` と `s3 cp` では、`--acl` オプションを使用できます。これにより、Amazon S3 にコピーされたファイルのアクセス権限を設定できます。`--acl` オプションは、`private`、`public-read`、および `public-read-write` の値を受け入れます。詳細については、「*Amazon S3 ユーザーガイド*」の「[既定 ACL](https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html#canned-acl)」を参照してください。  

```
$ aws s3 sync . s3://amzn-s3-demo-bucket/path --acl public-read
```

**exclude**  
`s3 cp`、`s3 mv`、`s3 sync`、`s3 rm` のいずれかのコマンドを使用する場合、`--exclude` オプションまたは `--include` オプションを使用して結果をフィルタリングできます。`--exclude` オプションは、コマンドからオブジェクトのみを除外するようにルールを設定します。各オプションは、指定した順に適用されます。これを次の例で示します。  

```
Local directory contains 3 files:
MyFile1.txt
MyFile2.rtf
MyFile88.txt

// Exclude all .txt files, resulting in only MyFile2.rtf being copied
$ aws s3 cp . s3://amzn-s3-demo-bucket/path --exclude "*.txt"

// Exclude all .txt files but include all files with the "MyFile*.txt" format, resulting in, MyFile1.txt, MyFile2.rtf, MyFile88.txt being copied
$ aws s3 cp . s3://amzn-s3-demo-bucket/path --exclude "*.txt" --include "MyFile*.txt"

// Exclude all .txt files, but include all files with the "MyFile*.txt" format, but exclude all files with the "MyFile?.txt" format resulting in, MyFile2.rtf and MyFile88.txt being copied
$ aws s3 cp . s3://amzn-s3-demo-bucket/path --exclude "*.txt" --include "MyFile*.txt" --exclude "MyFile?.txt"
```

**include**  
`s3 cp`、`s3 mv`、`s3 sync`、`s3 rm` のいずれかのコマンドを使用する場合、`--exclude` オプションまたは `--include` オプションを使用して結果をフィルタリングできます。`--include` オプションは、指定したオブジェクトのみをコマンドに含めるようにルールを設定します。各オプションは、指定した順に適用されます。これを次の例で示します。  

```
Local directory contains 3 files:
MyFile1.txt
MyFile2.rtf
MyFile88.txt

// Include all .txt files, resulting in MyFile1.txt and MyFile88.txt being copied
$ aws s3 cp . s3://amzn-s3-demo-bucket/path --include "*.txt"

// Include all .txt files but exclude all files with the "MyFile*.txt" format, resulting in no files being copied
$ aws s3 cp . s3://amzn-s3-demo-bucket/path --include "*.txt" --exclude "MyFile*.txt"

// Include all .txt files, but exclude all files with the "MyFile*.txt" format, but include all files with the "MyFile?.txt" format resulting in MyFile1.txt being copied

$ aws s3 cp . s3://amzn-s3-demo-bucket/path --include "*.txt" --exclude "MyFile*.txt" --include "MyFile?.txt"
```

**許可**  
`s3 cp`、`s3 mv`、`s3 sync` の各コマンドには、指定したユーザーやグループにオブジェクトへのアクセス許可を付与できる `--grants` オプションが含まれています。次の構文を使用して、`--grants` オプションをアクセス許可リストに設定します。`Permission`、`Grantee_Type`、`Grantee_ID` を独自の値に置き換えます。  
**構文**  

```
--grants Permission=Grantee_Type=Grantee_ID
         [Permission=Grantee_Type=Grantee_ID ...]
```
各値には以下の要素が含まれます。  
+ *Permission* - 付与されたアクセス許可を指定します。`read`、`readacl`、`writeacl`、`full` のいずれかに設定できます。
+ *Grantee\$1Type* - 被付与者の識別方法を指定します。`uri`、`emailaddress`、`id` のいずれかに設定できます。
+ *Grantee\$1ID* - *Grantee\$1Type* に基づいて被付与者を指定します。
  + `uri` – グループの URI。詳細については、「[被付与者とは](https://docs.aws.amazon.com/AmazonS3/latest/userguide/ACLOverview.html#SpecifyingGrantee)」を参照してください。
  + `emailaddress` – アカウントの E メールアドレス。
  + `id` – アカウントの正規 ID。
Amazon S3 アクセスコントロールの詳細については、「[アクセスコントロール](https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingAuthAccess.html)」を参照してください。  
次の例では、バケットにオブジェクトをコピーします。これは、オブジェクトへの `read` アクセス許可を全員に付与し、`full` アクセス許可 (`read`、`readacl`、`writeacl`) を `user@example.com` に関連付けられたアカウントに付与します。  

```
$ aws s3 cp file.txt s3://amzn-s3-demo-bucket/ --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers full=emailaddress=user@example.com
```
Amazon S3 にアップロードするオブジェクトについて、デフォルト以外のストレージクラス (`REDUCED_REDUNDANCY` または `STANDARD_IA`) を指定することもできます。そのためには、`--storage-class` オプションを使用します。  

```
$ aws s3 cp file.txt s3://amzn-s3-demo-bucket/ --storage-class REDUCED_REDUNDANCY
```

**no-overwrite**  
`s3 cp`、`s3 mv`、`s3 sync` コマンドには、送信先に既に存在するオブジェクトの上書きを防ぐために使用できる `--no-overwrite` オプションが含まれています。  
次の例では、オブジェクトがローカルディレクトリにまだ存在しない場合にのみ、バケットからローカルディレクトリにオブジェクトをコピーします。  

```
$ aws s3 cp --no-overwrite s3://amzn-s3-demo-bucket/file.txt file.txt
```
以下の例では、ローカルディレクトリからバケットにファイルを再帰的にコピーします。バケットにまだ存在しないファイルのみをコピーします。  

```
$ aws s3 cp --recursive --no-overwrite /path/to/demo-files/ s3://amzn-s3-demo-bucket/demo-files/
```
次の例では、オブジェクトがバケットの送信先ロケーションにまだ存在しない場合にのみ、ローカルディレクトリからバケットにオブジェクトを移動します。  

```
$ aws s3 mv --no-overwrite file.txt s3://amzn-s3-demo-bucket/file.txt
```
次の例では、ローカルディレクトリからバケットにファイルを同期します。送信先バケットにまだ存在しないファイルのみが同期されます。  

```
$ aws s3 sync --no-overwrite /path/to/demo-files/ s3://amzn-s3-demo-bucket/demo-files/
```

**再帰的**  
このオプションを使用すると、指定のディレクトリ内またはプレフィックス内のすべてのファイルやオブジェクトに対してコマンドが実行されます。次の例では、`s3://amzn-s3-demo-bucket/path` とそのすべての内容を削除します。  

```
$ aws s3 rm s3://amzn-s3-demo-bucket/path --recursive
```

## リソース
<a name="using-s3-commands-managing-buckets-references"></a>

**AWS CLI リファレンス:**
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3/index.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/mb.html](https://docs.aws.amazon.com/cli/latest/reference/s3/mb.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/mv.html](https://docs.aws.amazon.com/cli/latest/reference/s3/mv.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html](https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/rb.html](https://docs.aws.amazon.com/cli/latest/reference/s3/rb.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/rm.html](https://docs.aws.amazon.com/cli/latest/reference/s3/rm.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html](https://docs.aws.amazon.com/cli/latest/reference/s3/sync.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 オブジェクトの操作*」
+ 「[Amazon S3 ユーザーガイド](https://docs.aws.amazon.com//AmazonS3/latest/userguide/ListingKeysHierarchy.html)」の「*プレフィックスと区切り記号によるキーの階層的なリスト*」
+ 「*Amazon S3 ユーザーガイド*」の「[AWS SDK for .NET (低レベル) を使用した S3 バケットへのマルチパートアップロードの中止](https://docs.aws.amazon.com//AmazonS3/latest/userguide/LLAbortMPUnet.html)」

# Using API-Level (s3api) commands in the AWS CLI
<a name="cli-services-s3-apicommands"></a>

API レベルのコマンド (`s3api` コマンドセットに含まれる) は、Amazon Simple Storage Service (Amazon S3) API への直接アクセスを提供し、高レベルの `s3` コマンドでは公開されていない一部の操作を有効にします。これらのコマンドは、サービスの機能性への API レベルのアクセスを提供する他の AWS サービスに相当します。`s3` コマンドの詳細については、「[Using high-level (s3) commands in the AWS CLI](cli-services-s3-commands.md)」を参照してください。

このトピックでは、Amazon S3 API にマッピングする低レベルコマンドの使用方法を示す例を示します。さらに、各 S3 API コマンドの例については、「[AWS CLI バージョン 2 リファレンスガイド](https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html)」の「`s3api`」セクションで参照できます。

**Topics**
+ [前提条件](#cli-services-s3-apicommands-prereqs)
+ [カスタム ACL の適用](#cli-services-s3-apicommands-acls)
+ [ログ記録ポリシーの設定](#cli-services-s3-apicommands-logpol)
+ [リソース](#cli-services-s3-apicommands-resources)

## 前提条件
<a name="cli-services-s3-apicommands-prereqs"></a>

`s3api` コマンドを実行するには、次のことが必要です。
+ AWS CLI をインストールして設定します。詳細については、「[AWS CLI の最新バージョンのインストールまたは更新](getting-started-install.md)」および「」を参照してください。[Authentication and access credentials for the AWS CLI](cli-chap-authentication.md)
+ 使用するプロファイルには、例によって実行される AWS オペレーションに対する許可が必要です。
+ Amazon S3 の以下の用語を解説します。
  + **バケット** - トップレベルの Amazon S3 フォルダ。
  + **プレフィックス** - バケット内の Amazon S3 フォルダ。
  + **オブジェクト** - Amazon S3 バケットでホストされているすべてのアイテム。

## カスタム ACL の適用
<a name="cli-services-s3-apicommands-acls"></a>

高レベルコマンドでは、`--acl` オプションを使用して、事前定義されたアクセスコントロールリスト (ACL) を Amazon S3 オブジェクトに適用できます。ただし、そのコマンドを使用してバケット全体の ACL を設定することはできません。ただし、```[put-bucket-acl](https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-acl.html)` API レベルコマンドを使用することで、これを実行できます。

次の例は、完全なコントロールを 2 人の AWS ユーザー (*user1@example.com* と *user2@example.com*) に付与し、読み取りアクセス許可を everyone に付与する方法を示しています。「everyone」の識別子は、パラメータとして渡す特殊な URI から取得されます。

```
$ aws s3api put-bucket-acl --bucket amzn-s3-demo-bucket --grant-full-control 'emailaddress="user1@example.com",emailaddress="user2@example.com"' --grant-read 'uri="http://acs.amazonaws.com/groups/global/AllUsers"'
```

ACL の作成方法の詳細については、*Amazon シンプルストレージサービス API リファレンス*の「[PUT Bucket acl](https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTacl.html)」を参照してください。`s3api` など、CLI の `put-bucket-acl` ACL コマンドは、同様の[引数の略記法](https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-shorthand.html)を使用します。

## ログ記録ポリシーの設定
<a name="cli-services-s3-apicommands-logpol"></a>

API コマンド `put-bucket-logging` は、バケットのログ記録ポリシーを設定します。

次の例では、AWS ユーザー *user@example.com* にログファイルに対する完全なコントロールが付与され、すべてのユーザーがログファイルに対する読み取り許可を持っています。`put-bucket-acl` コマンドは、Amazon S3 ログ配信システム (URI で指定された) に、バケットへのログの読み取りと書き込みに必要なアクセス許可を付与するためにも必要です。

```
$ aws s3api put-bucket-acl --bucket amzn-s3-demo-bucket --grant-read-acp 'URI="http://acs.amazonaws.com/groups/s3/LogDelivery"' --grant-write 'URI="http://acs.amazonaws.com/groups/s3/LogDelivery"'
$ aws s3api put-bucket-logging --bucket amzn-s3-demo-bucket --bucket-logging-status file://logging.json
```

前のコマンドで `logging.json` ファイルの内容は、次のとおりです。

```
{
  "LoggingEnabled": {
    "TargetBucket": "amzn-s3-demo-bucket",
    "TargetPrefix": "amzn-s3-demo-bucketLogs/",
    "TargetGrants": [
      {
        "Grantee": {
          "Type": "AmazonCustomerByEmail",
          "EmailAddress": "user@example.com"
        },
        "Permission": "FULL_CONTROL"
      },
      {
        "Grantee": {
          "Type": "Group",
          "URI": "http://acs.amazonaws.com/groups/global/AllUsers"
        },
        "Permission": "READ"
      }
    ]
  }
}
```

## リソース
<a name="cli-services-s3-apicommands-resources"></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/put-bucket-acl.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-acl.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-logging.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/put-bucket-logging.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 オブジェクトの操作*」
+ 「[Amazon S3 ユーザーガイド](https://docs.aws.amazon.com//AmazonS3/latest/userguide/ListingKeysHierarchy.html)」の「*プレフィックスと区切り記号によるキーの階層的なリスト*」
+ 「*Amazon S3 ユーザーガイド*」の「[AWS SDK for .NET (低レベル) を使用した S3 バケットへのマルチパートアップロードの中止](https://docs.aws.amazon.com//AmazonS3/latest/userguide/LLAbortMPUnet.html)」

# 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/)を参照してください。