You need to enable JavaScript to run this app.
导航
删除场景(Python SDK)
最近更新时间:2024.02.04 18:31:03首次发布时间:2023.01.19 18:21:16

本文介绍删除对象的常用场景代码。

注意事项

  • 要删除对象,您的账号必须具备 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))