版本控制应用于桶中所有对象。通过开启桶的版本控制,您可以在错误删除或者覆盖对象后,将对象恢复至任意的历史版本。目前支持多版本的接口包含 GetObject、GetObjectACL、CopyObject、UploadPartCopy、DeleteObject等。调用相关接口时,您可通过可选参数 version_id 指定操作对象的具体版本。
TOS中桶的版本状态包含未开启、开启版本控制和暂停版本控制三种。
注意
tos:PutBucketVersioning
权限。tos:GetBucketVersioning
权限。以下代码用于设置桶状态为开启多版本(Enable)状态或暂停版本控制状态(Suspended)。
import os import tos from tos import VersioningStatusType # 从环境变量获取 AK 和 SK 信息。 ak = os.getenv('TOS_ACCESS_KEY') sk = os.getenv('TOS_SECRET_KEY') endpoint = "your endpoint" region = "your region" bucket_name = "bucket-test" object_key = "object-test" try: # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现 client = tos.TosClientV2(ak, sk, endpoint, region) # 开启桶多版本 client.put_bucket_versioning(bucket_name, VersioningStatusType.Versioning_Status_Enabled) # 暂停桶多版本 client.put_bucket_versioning(bucket_name, VersioningStatusType.Versioning_Status_Suspended) except tos.exceptions.TosClientError as e: # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常 print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause)) except tos.exceptions.TosServerError as e: # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息 print('fail with server error, code: {}'.format(e.code)) # request id 可定位具体问题,强烈建议日志中保存 print('error with request id: {}'.format(e.request_id)) print('error with message: {}'.format(e.message)) print('error with http code: {}'.format(e.status_code)) print('error with ec: {}'.format(e.ec)) print('error with request url: {}'.format(e.request_url)) except Exception as e: print('fail with unknown error: {}'.format(e))
以下代码用于获取桶 bucket-test
的版本控制状态信息。
import os import tos from tos import VersioningStatusType # 从环境变量获取 AK 和 SK 信息。 ak = os.getenv('TOS_ACCESS_KEY') sk = os.getenv('TOS_SECRET_KEY') endpoint = "your endpoint" region = "your region" bucket_name = "bucket-test" object_key = "object-test" try: # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现 client = tos.TosClientV2(ak, sk, endpoint, region) # 开启桶多版本 version_info = client.get_bucket_version(bucket_name) print('bucket version state:', version_info.status) except tos.exceptions.TosClientError as e: # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常 print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause)) except tos.exceptions.TosServerError as e: # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息 print('fail with server error, code: {}'.format(e.code)) # request id 可定位具体问题,强烈建议日志中保存 print('error with request id: {}'.format(e.request_id)) print('error with message: {}'.format(e.message)) print('error with http code: {}'.format(e.status_code)) print('error with ec: {}'.format(e.ec)) print('error with request url: {}'.format(e.request_url)) except Exception as e: print('fail with unknown error: {}'.format(e))
以下代码用于下载桶 bucket-test
中的对象 object-test
的指定版本到内存。
import os import tos # 从环境变量获取 AK 和 SK 信息。 ak = os.getenv('TOS_ACCESS_KEY') sk = os.getenv('TOS_SECRET_KEY') endpoint = "your endpoint" region = "your region" bucket_name = "bucket-test" object_key = "object-test" version_id = "your version id" try: # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现 client = tos.TosClientV2(ak, sk, endpoint, region) # get_object返回的是一个可迭代对象 object_stream = client.get_object(bucket_name, object_key, version_id=version_id) # 迭代读取对象内容 for content in object_stream: print(content) except tos.exceptions.TosClientError as e: # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常 print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause)) except tos.exceptions.TosServerError as e: # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息 print('fail with server error, code: {}'.format(e.code)) # request id 可定位具体问题,强烈建议日志中保存 print('error with request id: {}'.format(e.request_id)) print('error with message: {}'.format(e.message)) print('error with http code: {}'.format(e.status_code)) print('error with ec: {}'.format(e.ec)) print('error with request url: {}'.format(e.request_url)) except Exception as e: print('fail with unknown error: {}'.format(e))
以下代码用于删除指定桶 bucket-test
中的 object-test
对象的指定版本。
import os import tos # 从环境变量获取 AK 和 SK 信息。 ak = os.getenv('TOS_ACCESS_KEY') sk = os.getenv('TOS_SECRET_KEY') endpoint = "your endpoint" region = "your region" bucket_name = "bucket-test" object_key = "object-test" version_id = "your version id" try: # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现 client = tos.TosClientV2(ak, sk, endpoint, region) # 删除指定桶下的指定对象 resp = client.delete_object(bucket_name, object_key, version_id=version_id) except tos.exceptions.TosClientError as e: # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常 print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause)) except tos.exceptions.TosServerError as e: # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息 print('fail with server error, code: {}'.format(e.code)) # request id 可定位具体问题,强烈建议日志中保存 print('error with request id: {}'.format(e.request_id)) print('error with message: {}'.format(e.message)) print('error with http code: {}'.format(e.status_code)) print('error with ec: {}'.format(e.ec)) print('error with request url: {}'.format(e.request_url)) except Exception as e: print('fail with unknown error: {}'.format(e))
以下代码用于清空整个开启多版本桶的示例代码,包括删除对象的多个版本数据、删除删除标记对象数据、删除所有未合并的对象。
import os import tos # 从环境变量获取 AK 和 SK 信息。 ak = os.getenv('TOS_ACCESS_KEY') sk = os.getenv('TOS_SECRET_KEY') endpoint = "your endpoint" region = "your region" bucket_name = "bucket-test" try: # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现 client = tos.TosClientV2(ak, sk, endpoint, region) # 删除所有对象的所有版本、删除删除标记的所有版本 is_truncated = True key_marker = '' version_id_marker = '' while is_truncated: out = client.list_object_versions(bucket_name, key_marker=key_marker, version_id_marker=version_id_marker) # version 中包含多版本对象信息 for version in out.versions: client.delete_object(bucket_name, version.key, version.version_id) # 删除标记信息 for delete_marker in out.delete_markers: client.delete_object(bucket_name, delete_marker.key, delete_marker.version_id) is_truncated = out.is_truncated key_marker = out.next_key_marker version_id_marker = out.next_version_id_marker # 删除所有未合并的对象 is_truncated = True marker = '' while is_truncated: out = client.list_multipart_uploads(bucket_name, key_marker=marker) for upload in out.uploads: client.abort_multipart_upload(bucket_name, upload.key, upload.upload_id) is_truncated = out.is_truncated marker = out.next_key_marker except tos.exceptions.TosClientError as e: # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常 print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause)) except tos.exceptions.TosServerError as e: # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息 print('fail with server error, code: {}'.format(e.code)) # request id 可定位具体问题,强烈建议日志中保存 print('error with request id: {}'.format(e.request_id)) print('error with message: {}'.format(e.message)) print('error with http code: {}'.format(e.status_code)) print('error with ec: {}'.format(e.ec)) print('error with request url: {}'.format(e.request_url)) except Exception as e: print('fail with unknown error: {}'.format(e))