Python數(shù)據(jù)復(fù)制
數(shù)據(jù)復(fù)制是以異步(近實時)方式將源Bucket中的文件(Object)以及對Object的創(chuàng)建、更新和刪除等操作自動復(fù)制到目標Bucket。OSS支持跨區(qū)域復(fù)制(Cross-Region Replication)和同區(qū)域復(fù)制(Same-Region Replication)。
注意事項
本文以華東1(杭州)外網(wǎng)Endpoint為例。如果您希望通過與OSS同地域的其他阿里云產(chǎn)品訪問OSS,請使用內(nèi)網(wǎng)Endpoint。關(guān)于OSS支持的Region與Endpoint的對應(yīng)關(guān)系,請參見訪問域名和數(shù)據(jù)中心。
本文以從環(huán)境變量讀取訪問憑證為例。如何配置訪問憑證,請參見配置訪問憑證。
本文以O(shè)SS域名新建OSSClient為例。如果您希望通過自定義域名、STS等方式新建OSSClient,請參見初始化。
要開啟數(shù)據(jù)復(fù)制,您必須有
oss:PutBucketReplication
權(quán)限;要查看數(shù)據(jù)復(fù)制規(guī)則,您必須有oss:GetBucketReplication
權(quán)限;要查看可復(fù)制的目標地域,您必須有oss:GetBucketReplicationLocation
權(quán)限;要查看數(shù)據(jù)復(fù)制進度,您必須有oss:GetBucketReplicationProgress
權(quán)限;要關(guān)閉數(shù)據(jù)復(fù)制,您必須有oss:DeleteBucketReplication
權(quán)限。具體操作,請參見為RAM用戶授權(quán)自定義的權(quán)限策略。
開啟數(shù)據(jù)復(fù)制
開啟數(shù)據(jù)復(fù)制前,請確保源存儲空間與目標存儲空間同時處于非版本化或已啟用版本控制狀態(tài)。
以下代碼用于開啟數(shù)據(jù)復(fù)制,將華東1(杭州)地域下的srcexamplebucket中的數(shù)據(jù)復(fù)制到相同或不同地域下的destexamplebucket。
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import ReplicationRule
# 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# 填寫源Bucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。
# 填寫源Bucket名稱,例如srcexamplebucket。
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'srcexamplebucket')
replica_config = ReplicationRule(
# 指定數(shù)據(jù)要復(fù)制到的目標Bucket。
target_bucket_name='destexamplebucket',
# 指定目標Bucket所在地域。
# 如果您希望開啟跨區(qū)域復(fù)制,則源Bucket與目標Bucket必須處于不同的地域。如果您希望開啟同地域復(fù)制,則源Bucket與目標Bucket必須處于相同的地域。
target_bucket_location='yourTargetBucketLocation'
)
# 指定待復(fù)制Object的前綴Prefix。指定Prefix后,只有匹配該Prefix的Object才會復(fù)制到目標Bucket。
# prefix_list = ['prefix1', 'prefix2']
# 設(shè)置數(shù)據(jù)復(fù)制規(guī)則。
# replica_config = ReplicationRule(
# prefix_list=prefix_list,
# 指定可以被復(fù)制到目標Bucket的操作。默認值為ALL,表示源Bucket的所有操作都會復(fù)制到目標Bucket。
# action_list=[ReplicationRule.ALL],
# 指定數(shù)據(jù)要復(fù)制到的目標Bucket。
# target_bucket_name='destexamplebucket1',
# 指定目標Bucket所在地域。
# target_bucket_location='yourTargetBucketLocation',
# 默認復(fù)制歷史數(shù)據(jù)。此處設(shè)置為False,表示禁止復(fù)制歷史數(shù)據(jù)。
# is_enable_historical_object_replication=False,
# 指定數(shù)據(jù)復(fù)制時使用的數(shù)據(jù)傳輸鏈路。
# target_transfer_type='oss_acc',
# 授權(quán)OSS使用哪個角色來進行數(shù)據(jù)復(fù)制。如果指定使用SSE-KMS加密目標對象,則必須指定該元素。
# sync_role_name='roleNameTest',
# 復(fù)制通過SSE-KMS加密創(chuàng)建的對象。
# sse_kms_encrypted_objects_status=ReplicationRule.ENABLED
# 指定SSE-KMS密鑰ID。如果指定復(fù)制通過SSE-KMS加密創(chuàng)建的對象,則必須指定該元素。
# replica_kms_keyid='9468da86-3509-4f8d-a61e-6eab1eac****',
#)
# 開啟數(shù)據(jù)復(fù)制。
bucket.put_bucket_replication(replica_config)
查看數(shù)據(jù)復(fù)制規(guī)則
以下代碼用于查看examplebucket的數(shù)據(jù)復(fù)制規(guī)則。
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import ReplicationRule
# 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。
# 填寫B(tài)ucket名稱,例如examplebucket。
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
# 查看數(shù)據(jù)復(fù)制規(guī)則。
result = bucket.get_bucket_replication()
# 打印返回的信息。
for rule in result.rule_list:
print(rule.rule_id)
print(rule.target_bucket_name)
print(rule.target_bucket_location)
查看可復(fù)制的目標地域
以下代碼用于查看examplebucket的數(shù)據(jù)可復(fù)制的目標地域列表。
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import ReplicationRule
# 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。
# 填寫B(tài)ucket名稱,例如examplebucket。
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
# 查看可復(fù)制的目標地域。
result = bucket.get_bucket_replication_location()
for location in result.location_list:
print(location)
查看數(shù)據(jù)復(fù)制進度
數(shù)據(jù)復(fù)制進度分為歷史數(shù)據(jù)復(fù)制進度和新寫入數(shù)據(jù)復(fù)制進度。
歷史數(shù)據(jù)復(fù)制進度用百分比表示,僅對開啟了歷史數(shù)據(jù)復(fù)制的存儲空間有效。
新寫入數(shù)據(jù)復(fù)制進度用新寫入數(shù)據(jù)的時間點表示,代表這個時間點之前的數(shù)據(jù)已復(fù)制完成。
以下代碼用于查看examplebucket中規(guī)則ID為test_replication_1的數(shù)據(jù)復(fù)制進度。
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import ReplicationRule
# 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。
# 填寫B(tài)ucket名稱,例如examplebucket。
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
# 查看數(shù)據(jù)復(fù)制進度。
# 填寫復(fù)制規(guī)則ID,例如test_replication_1。
result = bucket.get_bucket_replication_progress('test_replication_1')
print(result.progress.rule_id)
# 是否開啟了歷史數(shù)據(jù)復(fù)制。
print(result.progress.is_enable_historical_object_replication)
# 歷史數(shù)據(jù)復(fù)制進度。
print(result.progress.historical_object_progress)
# 實時數(shù)據(jù)復(fù)制進度。
print(result.progress.new_object_progress)
關(guān)閉數(shù)據(jù)復(fù)制
通過刪除存儲空間的復(fù)制規(guī)則,您可以關(guān)閉源存儲空間到目標存儲空間的數(shù)據(jù)復(fù)制關(guān)系。
以下代碼用于刪除examplebucket中規(guī)則ID為test_replication_1的復(fù)制規(guī)則。
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import ReplicationRule
# 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。
# 填寫B(tài)ucket名稱,例如examplebucket。
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
# 關(guān)閉數(shù)據(jù)復(fù)制。關(guān)閉后,已復(fù)制到目標Bucket內(nèi)的文件依然存在,但是不再將源Bucket內(nèi)文件的所有改動復(fù)制到目標Bucket。
# 填寫復(fù)制規(guī)則ID,例如test_replication_1。
result = bucket.delete_bucket_replication('test_replication_1')
相關(guān)文檔
關(guān)于數(shù)據(jù)復(fù)制的完整示例代碼,請參見GitHub。
關(guān)于開啟數(shù)據(jù)復(fù)制的API接口說明,請參見PutBucketReplication。
關(guān)于查看數(shù)據(jù)復(fù)制規(guī)則的API接口說明,請參見GetBucketReplication。
關(guān)于查看可復(fù)制的目標地域的API接口說明,請參見GetBucketReplicationLocation。
關(guān)于查看數(shù)據(jù)復(fù)制進度的API接口說明,請參見GetBucketReplicationProgress。
關(guān)于關(guān)閉數(shù)據(jù)復(fù)制的API接口說明,請參見DeleteBucketReplication。