Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Data replication (Python SDK V1)

Son Güncelleme:Oct 01, 2026

Veri çoğaltma, nesneleri ve bunların oluşturma, güncelleme ve silme işlemlerini kaynak paketten hedef pakete neredeyse gerçek zamanlı olarak eşzamansız biçimde kopyalar. OSS, bölge arası çoğaltmayı (CRR) ve aynı bölge içinde çoğaltmayı (SRR) destekler.

Notlar

  • Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek için dahili uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.

  • Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. OSS SDK for Python 1.0 kullanarak erişim kimlik bilgilerini yapılandırma.

  • Bu konu, bir OSS uç noktası kullanarak bir OSSClient örneği oluşturmayı gösterir. Özel etki alanı kullanmak veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulaması yapmak gibi alternatif yapılandırmalar için bkz. Başlatma.

  • Veri çoğaltmayı etkinleştirmek için oss:PutBucketReplication iznine sahip olmanız gerekir. Veri çoğaltma kurallarını sorgulamak için oss:GetBucketReplication iznine sahip olmanız gerekir. Verilerin çoğaltılabileceği bölgeleri sorgulamak için oss:GetBucketReplicationLocation iznine sahip olmanız gerekir. Bir veri çoğaltma görevinin ilerleme durumunu sorgulamak için oss:GetBucketReplicationProgress iznine sahip olmanız gerekir. Veri çoğaltmayı devre dışı bırakmak için oss:DeleteBucketReplication iznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.

Veri çoğaltmayı etkinleştirin

Önemli

Veri çoğaltmayı etkinleştirmeden önce kaynak ve hedef paketlerin her ikisinin de sürümsüz olduğunu veya sürüm oluşturmanın etkinleştirildiğini doğrulayın.

Aşağıdaki kod, Çin (Hangzhou) bölgesindeki srcexamplebucket paketinden aynı veya farklı bir bölgedeki destexamplebucket paketine veri çoğaltmayı etkinleştirir.

# -*- 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)

Veri çoğaltma kurallarını görüntüleyin

Aşağıdaki kod, examplebucket paketi için veri çoğaltma kurallarını sorgular.

# -*- 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)

Çoğaltma için kullanılabilir hedef bölgeleri görüntüleyin

Aşağıdaki kod, examplebucket paketindeki verilerin çoğaltılabileceği hedef bölgeleri listeler.

# -*- 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)

Veri çoğaltma ilerleme durumunu görüntüleyin

Geçmiş veri çoğaltma görevlerinin ve artımlı veri çoğaltma görevlerinin ilerleme durumunu sorgulayabilirsiniz.

  • Geçmiş veri çoğaltma görevlerinin ilerleme durumu yüzde olarak ifade edilir. Geçmiş veri çoğaltma görevlerinin ilerleme durumunu yalnızca geçmiş veri çoğaltmanın etkinleştirildiği paketler için sorgulayabilirsiniz.

  • Artımlı veri çoğaltma görevlerinin ilerleme durumu bir zaman noktası olarak ifade edilir. Kaynak pakette bu zaman noktasından önce depolanan veriler çoğaltılır.

Aşağıdaki kod, examplebucket paketinde test_replication_1 kimlikli kural için veri çoğaltma ilerleme durumunu sorgular.

# -*- 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)            

Veri çoğaltmayı devre dışı bırakın

Kaynak paket ile hedef paket arasındaki veri çoğaltmayı devre dışı bırakmak için paketin çoğaltma kuralını silin.

Aşağıdaki kod, examplebucket paketinden test_replication_1 kimlikli çoğaltma kuralını siler.

# -*- 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')

İlgili konular

  • Veri çoğaltmaya ilişkin tam örnek kod için bkz. GitHub.

  • Veri çoğaltmayı etkinleştirmeye yönelik API işlemi hakkında daha fazla bilgi için bkz. PutBucketReplication.

  • Veri çoğaltma kurallarını görüntülemeye yönelik API işlemi hakkında daha fazla bilgi için bkz. GetBucketReplication.

  • Çoğaltma için kullanılabilir hedef bölgeleri görüntülemeye yönelik API işlemi hakkında daha fazla bilgi için bkz. GetBucketReplicationLocation.

  • Veri çoğaltma ilerleme durumunu görüntülemeye yönelik API işlemi hakkında daha fazla bilgi için bkz. GetBucketReplicationProgress.

  • Veri çoğaltmayı devre dışı bırakmaya yönelik API işlemi hakkında daha fazla bilgi için bkz. DeleteBucketReplication.