PutBucketReplication与 a 一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

PutBucketReplication与 a 一起使用 CLI

以下代码示例演示如何使用 PutBucketReplication

CLI
AWS CLI

为 S3 存储桶配置复制

以下 put-bucket-replication 示例将复制配置应用于指定的 S3 存储桶。

aws s3api put-bucket-replication \ --bucket AWSDOC-EXAMPLE-BUCKET1 \ --replication-configuration file://replication.json

replication.json 的内容:

{ "Role": "arn:aws:iam::123456789012:role/s3-replication-role", "Rules": [ { "Status": "Enabled", "Priority": 1, "DeleteMarkerReplication": { "Status": "Disabled" }, "Filter" : { "Prefix": ""}, "Destination": { "Bucket": "arn:aws:s3:::AWSDOC-EXAMPLE-BUCKET2" } } ] }

目标存储桶必须已启用版本控制。指定的角色必须具有写入目标存储桶的权限,并且必须建立允许 Amazon S3 代入角色的信任关系。

示例角色权限策略:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetReplicationConfiguration", "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::AWSDOC-EXAMPLE-BUCKET1" ] }, { "Effect": "Allow", "Action": [ "s3:GetObjectVersion", "s3:GetObjectVersionAcl", "s3:GetObjectVersionTagging" ], "Resource": [ "arn:aws:s3:::AWSDOC-EXAMPLE-BUCKET1/*" ] }, { "Effect": "Allow", "Action": [ "s3:ReplicateObject", "s3:ReplicateDelete", "s3:ReplicateTags" ], "Resource": "arn:aws:s3:::AWSDOC-EXAMPLE-BUCKET2/*" } ] }

示例信任关系策略:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "s3.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }

此命令不生成任何输出。

有关更多信息,请参阅《Amazon Simple Storage Service 控制台用户指南》中的这是主题标题

PowerShell
用于 PowerShell

示例 1:此示例使用一条规则设置复制配置,允许将存储桶 “examplebucket” 中使用密钥名称前缀 “” 创建的任何新对象复制到 “exampletargetbucketTaxDocs” 存储桶。

$rule1 = New-Object Amazon.S3.Model.ReplicationRule $rule1.ID = "Rule-1" $rule1.Status = "Enabled" $rule1.Prefix = "TaxDocs" $rule1.Destination = @{ BucketArn = "arn:aws:s3:::amzn-s3-demo-destination-bucket" } $params = @{ BucketName = "amzn-s3-demo-bucket" Configuration_Role = "arn:aws:iam::35667example:role/CrossRegionReplicationRoleForS3" Configuration_Rule = $rule1 } Write-S3BucketReplication @params

示例 2:此示例设置了具有多个规则的复制配置,允许将使用密钥名称前缀 “” 或 “” 创建的任何新对象复制到'exampletargetbu TaxDocs cket'存储桶。OtherDocs键前缀不得重叠。

$rule1 = New-Object Amazon.S3.Model.ReplicationRule $rule1.ID = "Rule-1" $rule1.Status = "Enabled" $rule1.Prefix = "TaxDocs" $rule1.Destination = @{ BucketArn = "arn:aws:s3:::amzn-s3-demo-destination-bucket" } $rule2 = New-Object Amazon.S3.Model.ReplicationRule $rule2.ID = "Rule-2" $rule2.Status = "Enabled" $rule2.Prefix = "OtherDocs" $rule2.Destination = @{ BucketArn = "arn:aws:s3:::amzn-s3-demo-destination-bucket" } $params = @{ BucketName = "amzn-s3-demo-bucket" Configuration_Role = "arn:aws:iam::35667example:role/CrossRegionReplicationRoleForS3" Configuration_Rule = $rule1,$rule2 } Write-S3BucketReplication @params

示例 3:此示例更新了指定存储桶上的复制配置,以禁用控制将密钥名称前缀 “” 的对象复制到存储桶 “exampletargetbucketTaxDocs” 的规则。

$rule1 = New-Object Amazon.S3.Model.ReplicationRule $rule1.ID = "Rule-1" $rule1.Status = "Disabled" $rule1.Prefix = "TaxDocs" $rule1.Destination = @{ BucketArn = "arn:aws:s3:::amzn-s3-demo-destination-bucket" } $params = @{ BucketName = "amzn-s3-demo-bucket" Configuration_Role = "arn:aws:iam::35667example:role/CrossRegionReplicationRoleForS3" Configuration_Rule = $rule1 } Write-S3BucketReplication @params
  • 有关API详细信息,请参阅 AWS Tools for PowerShell Cmdlet 参考PutBucketReplication中的。