edit-icon download-icon

Cross-region replication

Last Updated: Aug 08, 2018

Cross-region replication is used to automatically and asynchronously copy objects across buckets in different regions. Any changes (creation, replacement, and deletion) to objects in the source bucket will be synchronized to the target bucket. This function is used to meet the requirements of cross-region disaster recovery and data replication.

For more information about cross-region replication, see Manage cross-region replication.

Enable cross-region replication

Use the following code to enable cross-region replication:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. // Create an OSSClient instance.
  8. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  9. AddBucketReplicationRequest request = new AddBucketReplicationRequest(bucketName);
  10. request.setReplicationRuleID("<yourRuleId>");
  11. request.setTargetBucketName("<yourTargetBucketName>");
  12. // This example uses the target endpoint China (Beijing).
  13. request.setTargetBucketLocation("oss-cn-beijing");
  14. // Disable historical data replication. Historical data replication is enabled by default.
  15. request.setEnableHistoricalObjectReplication(false);
  16. ossClient.addBucketReplication(request);
  17. // Close your OSSClient.
  18. ossClient.shutdown();

View cross-region replication configurations

Use the following code to view the cross-region replication configurations for a bucket:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. // Create an OSSClient instance.
  8. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  9. List<ReplicationRule> rules = ossClient.getBucketReplication(bucketName);
  10. for (ReplicationRule rule : rules) {
  11. System.out.println(rule.getReplicationRuleID());
  12. System.out.println(rule.getTargetBucketLocation());
  13. System.out.println(rule.getTargetBucketName());
  14. }
  15. // Close your OSSClient.
  16. ossClient.shutdown();

View cross-region replication progress

Cross-region replication progress includes the progress of historical and real-time data synchronization.

  • Percent signs (%) indicate the progress of historical data synchronization. This function only works for buckets for which historical data synchronization has been enabled.
  • Time points (the point of latest time the data is written) indicate the progress of real-time data synchronization. If a time point is displayed, the data created before this time point has been synchronized.

Use the following code to view the cross-region replication progress:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. // Create an OSSClient instance.
  8. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  9. BucketReplicationProgress process = ossClient.getBucketReplicationProgress(bucketName, "<yourRuleId>");
  10. System.out.println(process.getReplicationRuleID());
  11. // Check whether historical data synchronization is enabled.
  12. System.out.println(process.isEnableHistoricalObjectReplication());
  13. // Print historical data synchronization progress.
  14. System.out.println(process.getHistoricalObjectProgress());
  15. // Print real-time data synchronization progress.
  16. System.out.println(process.getNewObjectProgress());
  17. // Close your OSSClient.
  18. ossClient.shutdown();

View the target regions for synchronization

Use the following code to obtain the target regions to which buckets can be synchronized:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. // Create an OSSClient instance.
  8. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  9. List<String> locations = ossClient.getBucketReplicationLocation(bucketName);
  10. for (String loc : locations) {
  11. System.out.println(loc);
  12. }
  13. // Close your OSSClient.
  14. ossClient.shutdown();

Disable cross-region replication

Use the following code to disable cross-region replication:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. // Create an OSSClient instance.
  8. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  9. // Disable cross-region replication. After this function is disabled, objects in the target bucket exist and all changes in the source objects cannot be synchronized.
  10. ossClient.deleteBucketReplication(bucketName, "<yourRuleId>");
  11. // Close your OSSClient.
  12. ossClient.shutdown();
Thank you! We've received your feedback.