Cross-region replication (CRR) can automatically replicate objects across buckets in different Object Storage Service (OSS) regions in asynchronous mode. CRR can also synchronize operations, for example, create, overwrite, and delete operations on objects, from a source bucket to a destination bucket. This feature can meet your requirements for geo-disaster recovery and data replication.

Enable CRR

The following code provides an example on how to enable CRR to replicate data from the srcexamplebucket bucket in the China (Hangzhou) region to the dstexamplebucket bucket in the China (Beijing) region.

Notice Before you enable CRR, make sure that both the source bucket and the destination bucket are in the unversioned or versioned state.
# -*- coding: utf-8 -*-
import oss2
from oss2.models import ReplicationRule
# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. For more information about the endpoints of other regions, see Regions and endpoints. 
# Specify the name of the source bucket. Example: srcexamplebucket. For more information about the naming conventions for buckets, see Bucket naming conventions. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'srcexamplebucket')
replica_config = ReplicationRule(
    # Specify the destination bucket to which the data is replicated. 
    target_bucket_name='destexamplebucket',
    # Specify the region in which the destination bucket is located. 
    target_bucket_location='oss-cn-beijing'
)

# Specify the prefix that is used to determine the object that you want to replicate. After you specify the prefix, only objects whose names contain the prefix are replicated to the destination bucket. 
# prefix_list = ['prefix1', 'prefix2']
# Specify CRR rules. 
# replica_config = ReplicationRule(
     # prefix_list=prefix_list,
     # Specify the operations that can be synchronized to the destination bucket. By default, the value is ALL, which indicates that all operations on the source bucket are synchronized to the destination bucket. 
     # action_list=[ReplicationRule.ALL],
     # Specify the destination bucket to which the data is replicated. 
     # target_bucket_name='destexamplebucket1',
     # Specify the region in which the destination bucket is located. 
     # target_bucket_location='oss-cn-shanghai',
     # Specify whether to synchronize historical data. By default, the historical data is synchronized. In this example, this parameter is set to False, which indicates that the historical data is not synchronized. 
     # is_enable_historical_object_replication=False,
     # Specify the link that is used to transfer data during data replication. 
     # target_transfer_type='oss_acc',
     # Specify the role that you authorize OSS to use to replicate data. If you want to use SSE-KMS to encrypt the objects that are replicated to the destination bucket, you must configure this parameter. 
     # sync_role_name='roleNameTest',
     # Replicate the objects that are encrypted by using SSE-KMS. 
     # sse_kms_encrypted_objects_status=ReplicationRule.ENABLED
     # Specify the customer master key (CMK) ID used in SSE-KMS. If you want to use SSE-KMS to encrypt the objects that are replicated to the destination bucket, you must configure this parameter. 
     # replica_kms_keyid='9468da86-3509-4f8d-a61e-6eab1eac****',
  #)

# Enable CRR. 
bucket.put_bucket_replication(replica_config)

Query CRR configurations

The following code provides an example on how to query the CRR configurations of the examplebucket bucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.models import ReplicationRule

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')

# Query the CRR configurations. 
result = bucket.get_bucket_replication()
# Display the returned information. 
for rule in result.rule_list:
    print(rule.rule_id)
    print(rule.target_bucket_name)
    print(rule.target_bucket_location)

Query the progress of data synchronization

You can query the progress of historical data synchronization and the progress of 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.

The following code provides an example on how to query the progress of data synchronization that is performed on the examplebucket bucket configured with the test_replication_1 replication rule:

# -*- coding: utf-8 -*-
import oss2
from oss2.models import ReplicationRule

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')

# Query the progress of the CRR task that is performed on the bucket. 
# Specify the replication rule ID. Example: test_replication_1. 
result = bucket.get_bucket_replication_progress('test_replication_1')
print(result.progress.rule_id)
# Check whether CRR is enabled for historical data in the bucket. 
print(result.progress.is_enable_historical_object_replication)
# Display the progress of historical data synchronization. 
print(result.progress.historical_object_progress)
# Display the progress of real-time data synchronization. 
print(result.progress.new_object_progress)
            

Query the regions in which CRR can be performed

The following code provides an example on how to query the regions to which data in the examplebucket bucket can be synchronized:

# -*- coding: utf-8 -*-
import oss2
from oss2.models import ReplicationRule

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')

# Query the regions to which data in the source bucket can be synchronized. 
result = bucket.get_bucket_replication_location()
for location in result.location_list:
    print(location)

Disable CRR

You can disable CRR for the specified source bucket by deleting the replication rule that is configured for the bucket.

The following code provides an example on how to delete the test_replication_1 replication rule that is configured for the examplebucket bucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.models import ReplicationRule

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')

# Disable CRR for the bucket. 
# Specify the replication rule ID. Example: test_replication_1. 
result = bucket.delete_bucket_replication('test_replication_1')

References

  • For more information about the API operation that you can call to enable CRR, see PutBucketReplication.
  • For more information about the API operation that you can call to query CRR configurations, see GetBucketReplication.
  • For more information about the API operation that you can call to query the regions in which CRR can be performed, see GetBucketReplicationLocation.
  • For more information about the API operation that you can call to query the progress of a CRR task, see GetBucketReplicationProgress.
  • For more information about the API operation that you can call to disable CRR, see DeleteBucketReplication.