Cross-region replication (CRR) provides automatic and asynchronous (near real-time) replication of objects across buckets in different Object Storage Service (OSS) regions. Operations such as the creation, overwriting, and deletion of objects can be synchronized from a source bucket to a destination bucket. This feature can meet your requirements for geo-disaster recovery and data replication.

Note For more information about CRR, see Cross-region replication in OSS Developer Guide.

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:

# -*- 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 Resource Access Management (RAM) user to call API operations or perform routine O&M. 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 source bucket is located and the name of the source bucket. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'srcexamplebucket')

# Specify the replication rule ID, the name of the destination bucket, the region in which the destination bucket is located, and whether to synchronize historical data. 
# If you do not set the rule_id parameter or you leave the rule_id parameter empty, OSS generates a unique value for this replication rule. 
# If the destination bucket is located in the China (Beijing) region, set target_bucket_location to oss-cn-beijing. 
# By default, OSS synchronizes historical data. If you set is_enable_historical_object_replication to false, historical data is not synchronized. 
replica_config = ReplicationRule(rule_id='test_replication_1',
                                 target_bucket_name='dstexamplebucket',
                                 target_bucket_location='oss-cn-beijing',
                                 is_enable_historical_object_replication=False
                                 )
# Enable CRR for the source bucket. 
bucket.put_bucket_replication(replica_config)

Query the CRR configurations of a bucket

The following code provides an example on how to query the information about a CRR task that is performed on 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 O&M. 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 of a bucket 
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.

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 O&M. 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 where 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 O&M. 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 for a bucket

You can disable CRR for a specified source bucket by deleting the replication rule configured for this bucket.

The following code provides an example on how to delete the test_replication_1 replication rule 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 O&M. 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 this bucket. 
# Specify the replication rule ID. Example: test_replication_1. 
result = bucket.delete_bucket_replication('test_replication_1')