La réplication des données copie de manière asynchrone les objets, ainsi que leurs opérations de création, de mise à jour et de suppression, d'un bucket source vers un bucket de destination, en temps quasi réel. OSS prend en charge la réplication inter-régions (CRR) et la réplication intra-région (SRR).
Notes
Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez Régions et points de terminaison.
Les identifiants d'accès utilisés dans cette rubrique sont extraits des variables d'environnement. Pour savoir comment configurer les identifiants d'accès, consultez Configurer les identifiants d'accès à l'aide du SDK OSS pour Python 1.0.
Cette rubrique illustre la création d'une instance
OSSClientavec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Initialisation.Pour activer la réplication des données, vous devez disposer de l'autorisation
oss:PutBucketReplication. Pour interroger les règles de réplication des données, vous devez disposer de l'autorisationoss:GetBucketReplication. Pour consulter les régions vers lesquelles les données peuvent être répliquées, vous devez disposer de l'autorisationoss:GetBucketReplicationLocation. Pour suivre la progression d'une tâche de réplication des données, vous devez disposer de l'autorisationoss:GetBucketReplicationProgress. Pour désactiver la réplication des données, vous devez disposer de l'autorisationoss:DeleteBucketReplication. Pour plus d'informations, consultez Accorder une stratégie personnalisée.
Activer la réplication des données
Avant d'activer la réplication des données, assurez-vous que les buckets source et de destination ne sont pas versionnés ou ont tous deux le versioning activé.
Le code suivant active la réplication des données de srcexamplebucket dans la région Chine (Hangzhou) vers destexamplebucket dans la même région ou dans une région différente.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import ReplicationRule
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Specify the bucket name, for example, examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
replica_config = ReplicationRule(
# Specify the destination bucket to which you want to replicate data.
target_bucket_name='destexamplebucket',
# Specify the region where the destination bucket is located.
# If you want to enable cross-region replication, the source and destination buckets must be in different regions. If you want to enable same-region replication, the source and destination buckets must be in the same region.
target_bucket_location='yourTargetBucketLocation'
)
# Specify the prefixes of the objects to replicate. After you specify prefixes, only objects that match the prefixes are replicated to the destination bucket.
# prefix_list = ['prefix1', 'prefix2']
# Set the data replication rule.
# replica_config = ReplicationRule(
# prefix_list=prefix_list,
# Replicate object creation and update operations in the source bucket to the destination bucket.
# action_list=[ReplicationRule.PUT],
# Specify the destination bucket to which you want to replicate data.
# target_bucket_name='destexamplebucket1',
# Specify the region where the destination bucket is located.
# target_bucket_location='yourTargetBucketLocation',
# By default, historical data is replicated. In this example, this parameter is set to False to disable historical data replication.
# is_enable_historical_object_replication=False,
# Specify the data transmission link for data replication.
# target_transfer_type='oss_acc',
# Specify the role that OSS is authorized to use for data replication. This element is required if you use SSE-KMS to encrypt destination objects.
# sync_role_name='roleNameTest',
# Replicate objects that are created with SSE-KMS encryption.
# sse_kms_encrypted_objects_status=ReplicationRule.ENABLED
# Specify the SSE-KMS key ID. This element is required if you replicate objects that are created with SSE-KMS encryption.
# replica_kms_keyid='9468da86-3509-4f8d-a61e-6eab1eac****',
#)
# Enable data replication.
bucket.put_bucket_replication(replica_config)
Consulter les règles de réplication des données
Le code suivant interroge les règles de réplication des données pour examplebucket.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import ReplicationRule
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Specify the bucket name, for example, examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# View the data replication rules.
result = bucket.get_bucket_replication()
# Print the returned information.
for rule in result.rule_list:
print(rule.rule_id)
print(rule.target_bucket_name)
print(rule.target_bucket_location)
Consulter les régions de destination disponibles pour la réplication
Le code suivant liste les régions de destination vers lesquelles les données d'examplebucket peuvent être répliquées.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import ReplicationRule
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Specify the bucket name, for example, examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# View the destination regions available for replication.
result = bucket.get_bucket_replication_location()
for location in result.location_list:
print(location)
Consulter la progression de la réplication des données
Vous pouvez interroger la progression des tâches de réplication des données historiques et des tâches de réplication des données incrémentielles.
La progression des tâches de réplication des données historiques est exprimée en pourcentage. Vous ne pouvez interroger la progression des tâches de réplication des données historiques que pour les buckets pour lesquels la réplication des données historiques est activée.
La progression des tâches de réplication des données incrémentielles est exprimée sous forme d'un instant précis. Les données stockées dans le bucket source avant cet instant sont répliquées.
Le code suivant interroge la progression de la réplication des données pour la règle dont l'ID est test_replication_1 dans examplebucket.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import ReplicationRule
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Specify the bucket name, for example, examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# View the data replication progress.
# Specify the replication rule ID, for example, test_replication_1.
result = bucket.get_bucket_replication_progress('test_replication_1')
print(result.progress.rule_id)
# Check whether historical data replication is enabled.
print(result.progress.is_enable_historical_object_replication)
# Historical data replication progress.
print(result.progress.historical_object_progress)
# Real-time data replication progress.
print(result.progress.new_object_progress)
Désactiver la réplication des données
Pour désactiver la réplication des données entre un bucket source et un bucket de destination, supprimez la règle de réplication du bucket.
Le code suivant supprime la règle de réplication dont l'ID est test_replication_1 d'examplebucket.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import ReplicationRule
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region information that corresponds to the endpoint, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Specify the bucket name, for example, examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Disable data replication. After this operation, replicated objects remain in the destination bucket, but new changes from the source bucket stop replicating.
# Specify the replication rule ID, for example, test_replication_1.
result = bucket.delete_bucket_replication('test_replication_1')
Références
Pour obtenir l'exemple de code complet relatif à la réplication des données, consultez GitHub.
Pour plus d'informations sur l'opération API permettant d'activer la réplication des données, consultez PutBucketReplication.
Pour plus d'informations sur l'opération API permettant de consulter les règles de réplication des données, consultez GetBucketReplication.
Pour plus d'informations sur l'opération API permettant de consulter les régions de destination disponibles pour la réplication, consultez GetBucketReplicationLocation.
Pour plus d'informations sur l'opération API permettant de consulter la progression de la réplication des données, consultez GetBucketReplicationProgress.
Pour plus d'informations sur l'opération API permettant de désactiver la réplication des données, consultez DeleteBucketReplication.