You need to enable JavaScript to run this app.
导航

删除桶(Python SDK)

最近更新时间2024.02.04 18:30:54

首次发布时间2021.12.31 17:38:35

当您不再需要保留某个桶(Bucket)时,可将其删除,以免产生额外费用。

注意事项

  • 桶删除后不可恢复,请谨慎操作。
  • 删除桶之前,您必须具备 tos:DeleteBucket 权限。具体操作,请参见权限配置指南

前提条件

删除桶之前,请确保您已经删除桶中所有数据。

  • 如果桶开启了版本控制,请确保已删除当前桶内所有当前版本和历史版本对象。具体操作,请参见删除对象
  • 如果桶中存在未合并的分片,请确保删除所有分片数据,具体操作,请参见删除分片
  • 如果桶中的对象较多,您可以通过生命周期规则,设置对象的批量删除。具体操作,请参见设置生命周期规则

示例代码

以下代码用于删除桶 bucket-test

import os
import tos

# 从环境变量获取 AK 和 SK 信息。
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
# your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例,your endpoint 填写 tos-cn-beijing.volces.com,your region 填写 cn-beijing。
endpoint = "your endpoint"
region = "your region"
bucket_name = "bucket-test"

try:
    client = tos.TosClientV2(ak, sk, endpoint, region)
    client.delete_bucket(bucket_name)
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))

相关文档

关于删除桶的 API 文档,请参见 DeleteBucket