Data replication automatically copies objects and object operations—including creation, overwriting, and deletion—from a source bucket to a destination bucket. Object Storage Service (OSS) supports two replication modes: cross-region replication (CRR) and same-region replication (SRR).
Usage notes
The sample code in this topic uses
cn-hangzhou(China (Hangzhou)) as the example region and connects through the public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For the full list of regions and endpoints, see Regions and endpoints.Alibaba Cloud accounts have all data replication permissions by default. RAM users and identities authenticated with Security Token Service (STS) temporary credentials must be granted the relevant permissions explicitly.
Operation Required permission Enable data replication oss:PutBucketReplicationEnable or disable RTC oss:PutBucketRtcQuery replication rules oss:GetBucketReplicationQuery destination regions oss:GetBucketReplicationLocationQuery replication progress oss:GetBucketReplicationProgressDisable data replication oss:DeleteBucketReplication
Prerequisites
Before you begin, ensure that you have:
Both the source bucket and the destination bucket in the unversioned or versioning-enabled state
The permissions listed in Usage notes for any operations your RAM user or STS identity needs to perform
Query replication locations
The following code retrieves the regions to which data in a bucket can be replicated, using oss.GetBucketReplicationLocationRequest. The response includes any transfer type constraints for each destination region.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="get bucket replication location sample")
# Add the command-line arguments.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
# Parse the command-line arguments.
args = parser.parse_args()
# Obtain access credentials from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configuration of the SDK.
cfg = oss.config.load_default()
# Specify the credential provider.
cfg.credentials_provider = credentials_provider
# Specify the region.
cfg.region = args.region
# If an endpoint is provided from the command line, update the endpoint in the configuration with the provided endpoint.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Create an OSS client.
client = oss.Client(cfg)
# Query the regions to which data in the specified bucket can be replicated.
result = client.get_bucket_replication_location(oss.GetBucketReplicationLocationRequest(
bucket=args.bucket,
))
# Display the basic response information.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' replication location: {result.replication_location},'
f' location transfer type constraint: {result.replication_location.location_transfer_type_constraint},'
f' locationrtc constraint: {result.replication_location.locationrtc_constraint},'
)
# If restrictions on destination regions for data replication exist, display region details and transfer types.
if result.replication_location.location_transfer_type_constraint.location_transfer_types:
for r in result.replication_location.location_transfer_type_constraint.location_transfer_types:
print(f'result: location: {r.location}, transfer types: {r.transfer_types}')
if __name__ == "__main__":
main()Query replication progress
OSS tracks two types of replication progress:
Historical replication: expressed as a percentage. Only available when historical object replication is enabled for the bucket.
Incremental replication: expressed as a point in time. All objects uploaded to the source bucket before this point in time have been replicated.
The following code queries the replication progress for a specific rule ID using oss.GetBucketReplicationProgressRequest.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="get bucket replication progress sample")
# Add the command-line arguments.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--rule_id', help='The ID of the data replication rule to query.', required=True)
def main():
# Parse the command-line arguments.
args = parser.parse_args()
# Obtain access credentials from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configuration of the SDK.
cfg = oss.config.load_default()
# Specify the credential provider.
cfg.credentials_provider = credentials_provider
# Specify the region.
cfg.region = args.region
# If an endpoint is provided from the command line, update the endpoint in the configuration with the provided endpoint.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Create an OSS client.
client = oss.Client(cfg)
# Query the data replication progress.
result = client.get_bucket_replication_progress(oss.GetBucketReplicationProgressRequest(
bucket=args.bucket,
rule_id=args.rule_id,
))
# Display the basic response information.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' replication progress: {result.replication_progress}')
# If the data replication rule exists, display rule details.
if result.replication_progress.rules:
for r in result.replication_progress.rules:
print(f'result: historical object replication: {r.historical_object_replication}, '
f'progress: {r.progress}, '
f'id: {r.id}, '
f'prefix set: {r.prefix_set}, '
f'action: {r.action}, '
f'destination: {r.destination}, '
f'status: {r.status}')
if __name__ == "__main__":
main()