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

管理对象元数据(Python SDK)

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

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

对象元信息是对象的属性描述,包括 HTTP 标准属性(HTTP Header)和用户自定义元数据(User Meta)两种。

设置自定义元数据

注意

  • 要设置对象元数据,您必须具备 tos:PutObject 权限,具体操作请参见权限配置指南
  • 设置用户自定义元数据时,英文字母自定义元数据 Key 只支持小写,不支持空格等特殊字符。

示例代码

您可以设置对象元信息,用于标识对象的用途或属性,以下代码用于设置对象的元数据。

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-key'
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)

    # 普通上传时设置对象元数据
    client.put_object(bucket_name, object_key,
                      # 通过meta选填字段设置用户自定义元数据
                      meta={'author': 'test1', '姓名': '张三'},
                      # 通过content_disposition设置HTTP标准属性
                      content_disposition='attachment; filename=123.txt',
                      # 通过content_type设置对象类型
                      content_type="text/plain")
    # 分片上传时设置对象元数据
    client.create_multipart_upload(bucket_name, object_key,
                                   # 通过meta选填字段设置用户自定义元数据
                                   meta={'author': 'test2', '姓名': '张三'},
                                   # 通过content_disposition设置HTTP标准属性
                                   content_disposition='attachment; filename=123.txt',
                                   # 通过content_type设置对象类型
                                   content_type="text/plain")
    # 通过set_object_meta接口设置对象元数据
    client.set_object_meta(bucket_name, object_key, meta={'author': 'test3', '姓名': '张三'},
                           # 通过content_disposition设置HTTP标准属性
                           content_disposition='attachment; filename=123.txt',
                           # 通过content_type设置对象类型
                           content_type="text/plain")
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 文档,请参见 SetObjectMeta

获取对象元数据

注意

获取对象元数据您必须具备 tos:GetObject 权限,具体操作请参见 IAM 策略概述

示例代码

可以通过 head_object 获取对象元信息,以下代码用于获取对象的元数据。

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-key'
try:
    # 创建 TosClientV2 对象,对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    # 获取对象元数据
    result = client.head_object(bucket_name, object_key)
    for key, value in result.meta.items():
        print('meta key', key)
        print('meta value', value)
    print('content-disposition', result.content_disposition)
    print('content-type', result.content_type)
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 文档,请参见 HeadObject