

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

# 使用 將資料從 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 帳戶中的目的地 Amazon S3 儲存貯體，無論是在相同 AWS 區域 或不同區域中。

來源 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`。不過，我們建議您將程式設計跨帳戶許可授予目的地帳戶，因為 ACLs可能難以管理多個物件。

**警告**  
此案例需要具有程式設計存取和長期登入資料的 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 帳戶 中的兩個作用中 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)。
+ 熟悉跨帳戶許可。

*限制*
+ 此模式涵蓋一次性遷移。對於需要持續自動將新物件從來源儲存貯體遷移到目的地儲存貯體的情況，您可以使用 [Amazon S3 批次複寫](https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-batch-replication-batch.html)。
+ 此模式使用暫時性和非持久性的工作階段登入資料 `AccessKeyId`(`SecretAccessKey`、 和 `SessionToken`)。輸出中的過期時間戳記會指出這些登入資料何時過期。角色已設定工作階段持續時間上限。如果工作階段過期，複製任務將會取消。

## Architecture
<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/zh_tw/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 服務 透過命令列 shell 中的命令與 互動。
+ [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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立 IAM 使用者並取得存取金鑰。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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 身分型政策。根據您的使用案例修改來源和目的地儲存貯體名稱。此身分型政策允許擔任此角色的使用者存取來源儲存貯體和目的地儲存貯體。如需詳細說明，請參閱 [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 Resource Name (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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立並連接 Amazon S3 儲存貯體政策。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立目的地 Amazon S3 儲存貯體。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
|  AWS CLI 使用新建立的使用者登入資料設定 。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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/zh_tw/prescriptive-guidance/latest/patterns/copy-data-from-an-s3-bucket-to-another-account-and-region-by-using-the-aws-cli.html)如需詳細資訊，請參閱[如何使用 AWS CLI 擔任 IAM 角色？](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/zh_tw/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 身分 （使用者、群組和角色） ](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 文件）