

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 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*

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

此模式描述了如何将数据从一个账户中的 AWS 源亚马逊简单存储服务 (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`。但是，我们建议您向目标账户授予编程跨账户权限，因为 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 Batch Replicati](https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-batch-replication-batch.html) on。
+ 此模式使用临时且非持久的会话证书（`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/zh_cn/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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建 IAM 用户并获取访问密钥。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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 资源名称（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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建并附加 Amazon S3 存储桶策略。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建目标 Amazon S3 存储桶。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
|  AWS CLI 使用新创建的用户凭据进行配置。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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_cn/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`可以使用 copy (`cp`) 或 synchronize [(](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_cn/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>
+ [创建亚马逊 S3 存储桶](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html)（亚马逊 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 文档）
+ [同步命令](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3/sync.html)（AWS CLI 文档）