There are more AWS SDK examples available in the AWS Doc SDK Examples
Use PutBucketReplication
with an AWS SDK or CLI
The following code examples show how to use PutBucketReplication
.
- CLI
-
- AWS CLI
-
To configure replication for an S3 bucket
The following
put-bucket-replication
example applies a replication configuration to the specified S3 bucket.aws s3api put-bucket-replication \ --bucket
AWSDOC-EXAMPLE-BUCKET1
\ --replication-configurationfile://replication.json
Contents of
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" } } ] }
The destination bucket must have versioning enabled. The specified role must have permission to write to the destination bucket and have a trust relationship that allows Amazon S3 to assume the role.
Example role permission policy:
{ "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/*" } ] }
Example trust relationship policy:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "s3.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }
This command produces no output.
For more information, see This is the topic title in the Amazon Simple Storage Service Console User Guide.
-
For API details, see PutBucketReplication
in AWS CLI Command Reference.
-
- PowerShell
-
- Tools for PowerShell
-
Example 1: This example sets a replication configuration with a single rule enabling replication to the 'exampletargetbucket' bucket any new objects created with the key name prefix "TaxDocs" in the bucket 'examplebucket'.
$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
Example 2: This example sets a replication configuration with multiple rules enabling replication to the 'exampletargetbucket' bucket any new objects created with either the key name prefix "TaxDocs" or "OtherDocs". The key prefixes must not overlap.
$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
Example 3: This example updates the replication configuration on the specified bucket to disable the rule controlling replication of objects with the key name prefix "TaxDocs" to the bucket 'exampletargetbucket'.
$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
-
For API details, see PutBucketReplication in AWS Tools for PowerShell Cmdlet Reference.
-