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.

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

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

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

Copy
import java.io.IOException; import java.util.HashMap; import java.util.Map; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.BucketReplicationConfiguration; import com.amazonaws.services.s3.model.ReplicationDestinationConfig; import com.amazonaws.services.s3.model.ReplicationRule; import com.amazonaws.services.s3.model.ReplicationRuleStatus; public class CrossRegionReplicationComplete { private static String sourceBucketName = "source-bucket"; private static String roleARN = "arn:aws-cn:iam::account-id:role/role-name"; private static String destinationBucketArn = "arn:aws-cn:s3:::destination-bucket"; public static void main(String[] args) throws IOException { AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); try { Map<String, ReplicationRule> replicationRules = new HashMap<String, ReplicationRule>(); replicationRules.put( "a-sample-rule-id", new ReplicationRule() .withPrefix("Tax/") .withStatus(ReplicationRuleStatus.Enabled) .withDestinationConfig( new ReplicationDestinationConfig() .withBucketARN(destinationBucketArn) .withStorageClass(StorageClass.Standard_Infrequently_Accessed) ) ); s3Client.setBucketReplicationConfiguration( sourceBucketName, new BucketReplicationConfiguration() .withRoleARN(roleARN) .withRules(replicationRules) ); BucketReplicationConfiguration replicationConfig = s3Client.getBucketReplicationConfiguration(sourceBucketName); ReplicationRule rule = replicationConfig.getRule("a-sample-rule-id"); System.out.println("Destination Bucket ARN : " + rule.getDestinationConfig().getBucketARN()); System.out.println("Prefix : " + rule.getPrefix()); System.out.println("Status : " + rule.getStatus()); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which" + " means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means"+ " the client encountered " + "a serious internal problem while trying to " + "communicate with Amazon S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } } }

跨区域复制

如何设置跨区域复制

本页内容: