You need to enable JavaScript to run this app.
导航
删除对象(Python SDK)
最近更新时间:2024.09.27 14:38:29首次发布时间:2021.12.31 17:38:36

当对象不再使用时,您可以删除对象,降低成本开销。目前 TOS Python SDK 支持删除桶中的单个对象、删除指定的多个对象,对于开启多版本的桶,你可指定删除对象的任意版本数据。

注意事项

  • 删除对象时,您必须具备 tos:DeleteObject 权限,具体操作请参见权限配置指南
  • 删除多版本对象时,您必须具备 tos:DeleteObjectVersion 权限,具体操作请参见权限配置指南
  • 当桶的多版本状态是开启时,如果删除对象时不指定对象的版本,不会删除对象,将会产生一个新的版本号,并插入删除标记(DeleteMarker);当桶的多版本状态是Suspended时,删除版本为null的对象,并产生一个版本为null的删除标记。多版本状态下,指定版本删除将删除指定版本号的对象。

示例代码

删除指定对象

以下代码用于删除指定桶 bucket-test 中的 obejct-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'
# 对象名称,例如 example_dir 下的 example_object.txt 文件,则填写为 example_dir/example_object.txt
object_key = 'object-key'
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    # 删除指定桶下的指定对象
    resp = client.delete_object(bucket_name, object_key)
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 中的 obejct-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'
# 对象名称,例如 example_dir 下的 example_object.txt 文件,则填写为 example_dir/example_object.txt
object_key = 'object-key'
# 对象版本
version_id = "your object version"
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    # 删除指定桶下的指定对象
    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))

批量删除对象

说明

  • 在开启版本控制的桶中,在调用 DeleteMultiObjects 接口来批量删除对象时,如果在 Delete 请求中未指定 versionId,将插入删除标记。如果指定了versionId,将永久删除该对象的指定版本。
  • 批量删除对象支持的响应方式可以通过quiet进行设置:
    • quietfalse时,是指在返回响应时,不管对象是否删除成功都将删除结果包含在响应里。
    • quiettrue时,是指在返回响应时,只返回删除失败的对象结果,没有返回的认为删除成功。

以下代码用于删除桶 bucket-test 指定对象名的多个对象。

import os
import tos

from tos.models2 import ObjectTobeDeleted

# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
endpoint = "your endpoint"
region = "your region"
bucket_name = 'bucket-test'
# 对象名称,例如 example_dir 下的 example_object.txt 文件,则填写为 example_dir/example_object.txt
object_key_1 = "your object key1"
object_key_2 = "your object key2"
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)

    # 批量删除指定桶下的指定对象
    objects = []
    objects.append(ObjectTobeDeleted(object_key_1))
    objects.append(ObjectTobeDeleted(object_key_2))
    # 若桶开启多版本则不指定对象版本,则为标删除
    # 未开启多版本的桶则直接删除
    delete_out = client.delete_multi_objects(bucket=bucket_name, objects=objects)

    # deleted 包含了删除成功的对象
    for deleted in delete_out.deleted:
        # 删除对象的key
        print('success deleted object key', deleted.key)
        # 删除删除对象的版本号
        print('success delete object version id', deleted.version_id)
        # delete_marker标记是否为标删对象
        print('is delete marker', deleted.delete_marker)
        # delete_marker_version_id为标删对应的版本ID
        print('delete marker version id', deleted.delete_marker_version_id)

    # error 中包含了删除失败的对象
    for err in delete_out.error:
        print('delete err key', err.key)
        print('delete err key version', err.version_id)
        print('delete err message', err.message)
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.models2 import ObjectTobeDeleted

# 从环境变量获取 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_1 = "your object key1"
version_id_1 = "your object_key_1 version id"
object_key_2 = "your object key2"
version_id_2 = "your object_key_2 version id"
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)

    # 批量删除指定桶下的指定对象
    objects = []
    objects.append(ObjectTobeDeleted(object_key_1, version_id_1))
    objects.append(ObjectTobeDeleted(object_key_2, version_id_2))
    # 桶开启多版本并指定删除对象的版本号,则为直接标删除
    delete_out = client.delete_multi_objects(bucket=bucket_name, objects=objects)

    # deleted 包含了删除成功的对象
    for deleted in delete_out.deleted:
        # 删除对象的key
        print('success deleted object key', deleted.key)
        # 删除删除对象的版本号
        print('success delete object version id', deleted.version_id)
        # delete_marker标记是否为标删对象
        print('is delete marker', deleted.delete_marker)
        # delete_marker_version_id为标删对应的版本ID
        print('delete marker version id', deleted.delete_marker_version_id)

    # error 中包含了删除失败的对象
    for err in delete_out.error:
        print('delete err key', err.key)
        print('delete err key version', err.version_id)
        print('delete err message', err.message)
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 中前缀为 log/ 的所有对象。

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'
prefix = "log/"

try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    is_truncated = True
    marker = ''
    while is_truncated:
        out = client.list_objects(bucket_name, prefix='log/', marker=marker)
        for obj in out.contents:
            client.delete_object(bucket_name, obj.key)    
        is_truncated = out.is_truncated
        marker = out.next_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))

相关文档