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

获取桶地域(Python SDK)

最近更新时间2024.03.19 20:54:15

首次发布时间2023.01.19 17:25:20

桶(Bucket)是 TOS 的全局唯一的命名空间,相当于数据的容器,用来储存对象(Object)数据。本文介绍如何获取桶所在的地域信息。

注意事项

默认只有桶的所有者才能查看桶所在的区域。

示例代码

以下代码用于获取桶 bucket-test 所在的地域(Region)。

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:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    # 获取桶地域位置
    out = client.get_bucket_location(bucket_name)
    print('region={}'.format(out.region))
    print('extranet_endpoint={}'.format(out.extranet_endpoint))
    print('intranet_endpoint={}'.format(out.intranet_endpoint))
except tos.exceptions.TosClientError as e:
    # 操作失败,捕获客户端异常,一般情况为非法请求参数或网络异常
    print('with client error, message:{}, cause: {}'.format(e.message, e.cause))
except tos.exceptions.TosServerError as e:
    # 操作失败,捕获服务端异常,可从返回信息中获取详细错误信息
    print('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('with unknown error: {}'.format(e))