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

管理对象标签(Python SDK)

最近更新时间2024.02.04 18:31:03

首次发布时间2023.01.19 18:21:16

TOS 支持为对象设置标签,并根据对象标签,完成特定对象的生命周期管理。本文介绍如何管理对象标签。

注意事项

  • 设置对象标签,您必须具有 tos:PutObjectTagging 权限,具体操作,请参见权限配置指南
  • 获取对象标签,您必须具有 tos:GetObjectTagging 权限,具体操作,请参见权限配置指南
  • 删除对象标签,您必须具有 tos:DeleteObjectTagging 权限,具体操作,请参见权限配置指南
  • 每个对象最多添加 10 个标签,并且同一对象的 Key 不能重复,关于对象标签的限制说明,请参见对象标签限制说明

设置对象标签

TOS 支持使用对象标签对桶中文件进行分类,您可以针对相同的对象标签设置生命周期规则。

示例代码

以下代码用于设置桶 bucket-test 对象 object-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"
object_key = "object-test"
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    # 设置 tags
    # 若桶开启了多版本可通过 version_id = 'your object version id' 指定对应版本对象
    tag1 = tos.models2.Tag('key1', 'value1')
    tag2 = tos.models2.Tag('key2', 'value2')
    client.put_object_tagging(bucket_name, object_key, [tag1, tag2])
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))

获取对象标签

设置对象标签后,您可以根据获取对象的标签信息。当桶开启版本控制时,TOS默认返回对象当前版本的标签信息,您可以通过指定对象的版本来获取指定版本的标签信息。

示例代码

以下代码用于获取桶 bucket-testobject-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"
object_key = "object-test"
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    # 获取对象标签
    # 若桶开启了多版本可通过 version_id = 'your object version id' 指定对应版本对象
    tags = client.get_object_tagging(bucket_name, object_key)
    for tag in tags.tag_set:
        print('tag_key', tag.key)
        print('tag_value', tag.value)
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))

删除对象标签

您可以根据需要删除不需要的标签信息。当桶开启多版本时,TOS 默认只删除对象当前版本的标签信息,您可以通过指定对象的版本号来删除指定版本的标签信息。

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"
object_key = "object-test"
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    # 删除对象标签
    # 若桶开启了多版本可通过 version_id = 'your object version id' 指定对应版本对象
    client.delete_object_tagging(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))

相关文档

关于对象标签的更多信息,请参见管理对象标签