

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# を使用して Amazon S3 バケットから別のアカウントとリージョンにデータをコピーする AWS CLI
<a name="copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli"></a>

*Appasaheb Bagali、Purushotham G K (Amazon Web Services)*

## 概要
<a name="copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli-summary"></a>

このパターンでは、 AWS アカウントのソース Amazon Simple Storage Service (Amazon S3) バケットから、同じ AWS リージョン または別のリージョンの別の AWS アカウントの宛先 Amazon S3 バケットにデータを移行する方法について説明します。

ソース Amazon S3 バケットは、アタッチされたリソースポリシーを使用して AWS Identity and Access Management (IAM) アクセスを許可します。ターゲットアカウントのユーザーは、ソースバケットに対する `PutObject` と `GetObject` 権限を持つロールを引き受ける必要があります。最後に、 `copy`および `sync` コマンドを実行して、送信元 Amazon S3 バケットから送信先 Amazon S3 バケットにデータを転送します。

アカウントは、Amazon S3 バケットにアップロードするオブジェクトを所有します。複数のアカウントやリージョンにわたってオブジェクトをコピーする場合は、ターゲットアカウントにコピーされたオブジェクトの所有権を付与します。オブジェクトの所有権は、「[アクセス制御リスト (ACL)](https://docs.aws.amazon.com/AmazonS3/latest/dev/S3_ACLs_UsingACLs.html)」を`bucket-owner-full-control`に変更することで変更できます。ただし、ACL は複数のオブジェクトでは管理が難しい場合があるため、プログラムによるクロスアカウント権限を移行先のアカウントに付与することをお勧めします。

**警告**  
このシナリオでは、プログラムによるアクセスと長期的な認証情報を持つ IAM ユーザーが必要ですが、これにはセキュリティ上のリスクがあります。このリスクを軽減するために、これらのユーザーにはタスクの実行に必要な権限のみを付与し、不要になったユーザーは削除することをお勧めします。アクセスキーは、必要に応じて更新できます。詳細については、IAM ドキュメントの[「アクセスキーの更新](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html#Using_RotateAccessKey)」を参照してください。

## 前提条件と制限
<a name="copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli-prereqs"></a>

*前提条件*
+ 同じ または異なる AWS アカウント で 2 つのアクティブな AWS リージョン。
+ ソースアカウントの既存の Amazon S3 バケット。 
+ 送信元または送信先の Amazon S3 バケットで[デフォルトの暗号化](https://docs.aws.amazon.com/AmazonS3/latest/dev/bucket-encryption.html)が有効になっている場合は、 AWS Key Management Service (AWS KMS) キーのアクセス許可を変更する必要があります。詳細については、このトピックの [AWS re:Post 記事](https://repost.aws/knowledge-center/s3-bucket-access-default-encryption)を参照してください。
+ クロスアカウントアクセス許可に精通している。

*制限事項*
+ このパターンは 1 回限りの移行を対象としています。レプリケート元バケットからレプリケート先バケットへの新しいオブジェクトの継続的かつ自動的な移行が必要なシナリオでは、[Amazon S3 バッチレプリケーション](https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-batch-replication-batch.html)を使用できます。
+ このパターンでは`AccessKeyId`、一時的で非永続的なセッション認証情報 (、`SecretAccessKey`、`SessionToken`) を使用します。出力の有効期限タイムスタンプは、これらの認証情報の有効期限を示します。ロールは、最大セッション期間で設定されます。セッションの有効期限が切れると、コピージョブはキャンセルされます。

## アーキテクチャ
<a name="copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli-architecture"></a>

 

![\[Amazon S3 データを別のアカウントまたはリージョンにコピーする\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/images/pattern-img/a574c26b-fdd9-4472-842b-b34c3eb2bfe9/images/5e4dec53-dfc8-478b-a7c4-503d63c8ac4e.png)


## ツール
<a name="copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli-tools"></a>
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) は、コマンドラインシェルのコマンド AWS のサービス を通じて を操作するのに役立つオープンソースツールです。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) は、誰を認証し、誰に使用する権限を付与するかを制御することで、 AWS リソースへのアクセスを安全に管理するのに役立ちます。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) は、あらゆる量のデータを保存、保護、取得できるクラウドベースのオブジェクトストレージサービスです。

## ベストプラクティス
<a name="copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli-best-practices"></a>
+ [IAM でのセキュリティのベストプラクティス](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html) (IAM ドキュメント)
+ [最小権限権限の適用](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege) (IAM ドキュメント)

## エピック
<a name="copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli-epics"></a>

### 送信先に IAM ユーザーとロールを作成する AWS アカウント
<a name="create-an-iam-user-and-role-in-the-destination-aws-account"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| IAM ユーザーを作成してアクセスキーを取得します。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli.html) | AWS DevOps | 
| アイデンティティベースの IAM ポリシーを作成します。 | 次のアクセス許可`S3MigrationPolicy`を使用して、 という名前の IAM アイデンティティベースのポリシーを作成します。ユースケースに応じて、ソースとターゲットのバケット名を変更してください。この ID ベースのポリシーにより、このロールを引き受けるユーザーは、ソースバケットとターゲットバケットにアクセスできます。詳細な手順については、[IAM ドキュメントの「IAM ポリシーの作成](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create-console.html)」を参照してください。<pre>{<br />    "Version": "2012-10-17",		 	 	 <br />    "Statement": [<br />        {<br />            "Effect": "Allow",<br />            "Action": [<br />                "s3:ListBucket",<br />                "s3:ListObjectsV2",<br />                "s3:GetObject",<br />                "s3:GetObjectTagging",<br />                "s3:GetObjectVersion",<br />                "s3:GetObjectVersionTagging"<br />            ],<br />            "Resource": [<br />                "arn:aws:s3:::amazon-s3-demo-source-bucket",<br />                "arn:aws:s3:::amazon-s3-demo-source-bucket/*"<br />            ]<br />        },<br />        {<br />            "Effect": "Allow",<br />            "Action": [<br />                "s3:ListBucket",<br />                "s3:PutObject",<br />                "s3:PutObjectAcl",<br />                "s3:PutObjectTagging",<br />                "s3:GetObjectTagging",<br />                "s3:ListObjectsV2",<br />                "s3:GetObjectVersion",<br />                "s3:GetObjectVersionTagging"<br />            ],<br />            "Resource": [<br />                "arn:aws:s3:::amazon-s3-demo-destination-bucket",<br />                "arn:aws:s3:::amazon-s3-demo-destination-bucket/*"<br />            ]<br />        }<br />    ]<br />}</pre> | AWS DevOps | 
| IAM ロールを作成します。 | 次の信頼ポリシーを使用して`S3MigrationRole`、 という名前の IAM ロールを作成します。ユースケースに合わせて、信頼ポリシー内のターゲット IAM ロールの Amazon リソースネーム (ARN) を変更します。この信頼ポリシーにより、新しく作成された IAM ユーザは `S3MigrationRole` を継承できるようになります。以前に作成した をアタッチします`S3MigrationPolicy`。詳細な手順については、IAM ドキュメントの「[IAM ユーザーにアクセス許可を委任するロールの作成](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-user.html)」を参照してください。<pre>{<br />    "Version": "2012-10-17",		 	 	 <br />    "Statement": [<br />        {<br />            "Effect": "Allow",<br />            "Principal": {<br />                "AWS": "arn:aws:iam::<destination_account>:user/<user_name>"<br />            },<br />            "Action": "sts:AssumeRole",<br />            "Condition": {}<br />        }<br />    ]<br />}</pre> | AWS DevOps | 

### ソースアカウントに Amazon S3 バケットポリシーを作成してアタッチする
<a name="create-and-attach-the-s3-bucket-policy-in-the-source-account"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| Amazon S3 バケットポリシーを作成してアタッチします。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli.html) | クラウド管理者 | 

### 送信先 Amazon S3 バケットを設定する
<a name="configure-the-destination-s3-bucket"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| 送信先 Amazon S3 バケットを作成します。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli.html) | クラウド管理者 | 

### 送信先 Amazon S3 バケットにデータをコピーする
<a name="copy-data-to-the-destination-s3-bucket"></a>


| タスク | 説明 | 必要なスキル | 
| --- | --- | --- | 
| 新しく作成したユーザー認証情報 AWS CLI を使用して を設定します。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli.html) | AWS DevOps | 
| Amazon S3 移行ロールを引き受けます。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli.html)詳細については、「 [を使用して IAM ロールを引き受け AWS CLI るにはどうすればよいですか？」を参照してください。](https://repost.aws/knowledge-center/iam-assume-role-cli) | AWS 管理者 | 
| レプリケート元バケットからレプリケート先バケットにデータをコピーして同期します。 | ロールを引き受ける`S3MigrationRole`と、コピー (`cp`) コマンドまたは[同期](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/sync.html) (`sync`) コマンドを使用してデータ[をコピー](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/cp.html)できます。コピー:<pre>aws s3 cp s3://amazon-s3-demo-source-bucket/ \<br />    s3://amazon-s3-demo-destination-bucket/ \<br />    --recursive --source-region SOURCE-REGION-NAME --region DESTINATION-REGION-NAME</pre>同期:<pre>aws s3 sync s3://amazon-s3-demo-source-bucket/ \<br />    s3://amazon-s3-demo-destination-bucket/ \<br />    --source-region SOURCE-REGION-NAME --region DESTINATION-REGION-NAME</pre> | クラウド管理者 | 

## トラブルシューティング
<a name="copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli-troubleshooting"></a>


| 問題 | ソリューション | 
| --- | --- | 
| `ListObjects` オペレーションを呼び出すときにエラーが発生しました (`AccessDenied`) | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/prescriptive-guidance/latest/patterns/copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli.html) | 

## 関連リソース
<a name="copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli-resources"></a>
+ [Amazon S3 バケットの作成](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html) (Amazon S3 ドキュメント)
+ [Amazon S3 バケットポリシーとユーザーポリシー](https://docs.aws.amazon.com/AmazonS3/latest/dev/using-iam-policies.html) (Amazon S3 ドキュメント)
+ [IAM ID (ユーザー、グループ、ロール)](https://docs.aws.amazon.com/IAM/latest/UserGuide/id.html?icmpid=docs_iam_console)(IAM ドキュメント)
+ [cp コマンド](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/cp.html) (AWS CLI ドキュメント)
+ [sync コマンド](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/sync.html) (AWS CLI ドキュメント)