

# 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)」