Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

如何使用适用于 .NET 的 AWS 开发工具包设置跨区域复制

源存储桶和目标存储桶由两个不同的 AWS 账户拥有时,可以使用 AWS CLI 或一个 AWS 开发工具包对源存储桶添加复制配置。不能使用控制台添加复制配置,因为在对源存储桶添加复制配置时,控制台不提供指定由另一个 AWS 账户拥有的目标存储桶的方法。有关更多信息,请参阅 如何设置跨区域复制

以下适用于 .NET 的 AWS 开发工具包代码示例首先向存储桶添加复制配置,然后检索该配置。您需要通过提供存储桶名称和 IAM 角色 ARN 来更新代码。有关如何创建和测试有效示例的说明,请参阅 运行 Amazon S3 .NET 代码示例

Copy
using System; using System.Collections.Generic; using Amazon.S3; using Amazon.S3.Model; namespace s3.amazon.com.docsamples { class CrossRegionReplication { static string sourceBucket = "source-bucket"; static string destinationBucketArn = "arn:aws-cn:s3:::destination-bucket"; static string roleArn = "arn:aws-cn:iam::account-id:role/role-name"; public static void Main(string[] args) { try { using (var client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) { EnableReplication(client); RetrieveReplicationConfiguration(client); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Check the provided AWS Credentials."); Console.WriteLine( "To sign up for service, go to http://aws.amazon.com/s3"); } else { Console.WriteLine( "Error occurred. Message:'{0}' when enabling notifications.", amazonS3Exception.Message); } } } static void EnableReplication(IAmazonS3 client) { ReplicationConfiguration replConfig = new ReplicationConfiguration { Role = roleArn, Rules = { new ReplicationRule { Prefix = "Tax", Status = ReplicationRuleStatus.Enabled, Destination = new ReplicationDestination { BucketArn = destinationBucketArn } } } }; PutBucketReplicationRequest putRequest = new PutBucketReplicationRequest { BucketName = sourceBucket, Configuration = replConfig }; PutBucketReplicationResponse putResponse = client.PutBucketReplication(putRequest); } private static void RetrieveReplicationConfiguration(IAmazonS3 client) { // Retrieve the configuration. GetBucketReplicationRequest getRequest = new GetBucketReplicationRequest { BucketName = sourceBucket }; GetBucketReplicationResponse getResponse = client.GetBucketReplication(getRequest); // Print. Console.WriteLine("Printing replication configuration information..."); Console.WriteLine("Role ARN: {0}", getResponse.Configuration.Role); foreach (var rule in getResponse.Configuration.Rules) { Console.WriteLine("ID: {0}", rule.Id); Console.WriteLine("Prefix: {0}", rule.Prefix); Console.WriteLine("Status: {0}", rule.Status); } } } }

跨区域复制

如何设置跨区域复制

本页内容: