本文介绍删除对象的常用场景代码。
tos:DeleteObject
权限,具体操作请参见权限配置指南。tos:DeleteObjectVersion
权限,具体操作请参见权限配置指南。以下代码用于删除桶 bucket-test
的指定目录 example-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" client = tos.TosClientV2(ak, sk, endpoint, region) is_truncated = True next_continuation_token = '' while is_truncated: out = client.list_objects_type2(bucket_name, prefix="example-test/") is_truncated = out.is_truncated next_continuation_token = out.next_continuation_token for content in out.contents: client.delete_object(bucket_name, content.key)
以下代码用于清空未开启多版本桶 bucket-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" # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现 client = tos.TosClientV2(ak, sk, endpoint, region) # 删除未开启多版本桶中所有对象 truncated = True continuation_token = '' while truncated: result = client.list_objects_type2(bucket_name, continuation_token=continuation_token) for iterm in result.contents: client.delete_object(bucket_name, iterm.key) truncated = result.is_truncated continuation_token = result.next_continuation_token # 删除所有未合并的对象 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
以下代码用于清空开启多版本的桶bucket-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" 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))