You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Manage objects
Manage object metadata (Python SDK)
Copy page
Download PDF
Manage object metadata (Python SDK)
Object metadata describes the properties of an object, including standard HTTP properties (HTTP Header) and user-defined metadata (User Meta).​
Set custom metadata​
warning​
  • When setting user-defined metadata, custom metadata keys in English letters support lowercase only and do not allow spaces or special characters.​
Example code​
You can set object metadata to identify the object's purpose or attributes. The following code sets the object's metadata.​
​
import os​
import tos​
​
# Retrieve AKSK information from environment variables.​
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:​
# Fill in "your endpoint" and "your region" with the corresponding Endpoint of the region where your Bucket is located. Taking the example of North China 2 (Beijing), fill in "your endpoint" with tos-cn-beijing.volces.com and "your region" with cn-beijing.​
client = tos.TosClientV2(ak, sk, endpoint, region)​
​
# Set object metadata during regular upload.​
client.put_object(bucket_name, object_key,​
# Setting user-defined metadata through optional meta fields​
meta={'author': 'test1', 'name': 'Bob'},​
# Setting HTTP standard attribute through content_disposition​
content_disposition='attachment; filename=123.txt',​
# Setting object type through content_type​
content_type="text/plain")​
# Set object metadata during multipart upload.​
client.create_multipart_upload(bucket_name, object_key,​
# Setting user-defined metadata through the optional fields in the meta section.​
meta={'author': 'test2', 'name': 'Bob'},​
# Setting HTTP standard attributes through content_disposition​
content_disposition='attachment; filename=123.txt',​
# Set the object type through the content_type.​
content_type="text/plain")​
# Set object metadata through the set_object_meta interface.​
client.set_object_meta(bucket_name, object_key, meta={'author': 'test3', 'name': 'Bob'},​
# Set HTTP standard properties through content_disposition​
content_disposition='attachment; filename=123.txt',​
# Set object type through content_type​
content_type="text/plain")​
except tos.exceptions.TosClientError as e:​
# Operation failed, catch client error, usually caused by invalid request parameters or network issues​
print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))​
except tos.exceptions.TosServerError as e:​
# Operation failed, catch server error, detailed error information can be obtained from the response​
print('fail with server error, code: {}'.format(e.code))​
# request id can be used to locate specific issues, it is strongly recommended to save it in the log.​
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))​
​
Related documentation​
For API documentation on setting object metadata, see SetObjectMeta.​
Retrieve object metadata​
warning​
To get object metadata, you must have tos:GetObject permission. For details, see IAM Policy Overview.​
Sample code​
You can use head_object to obtain object metadata. The following code retrieves the object's metadata.​
​
import os​
import tos​
​
# Retrieve AKSK information from environment variables.​
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:​
# Fill in "your endpoint" and "your region" with the corresponding Endpoint of the region where your Bucket is located. Taking the example of North China 2 (Beijing), fill in "your endpoint" with tos-cn-beijing.volces.com and "your region" with cn-beijing.​
client = tos.TosClientV2(ak, sk, endpoint, region)​
# Get object metadata.​
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:​
# Operation failed, catch client error, usually caused by invalid request parameters or network issues​
print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))​
except tos.exceptions.TosServerError as e:​
# Operation failed, catch server error, detailed error information can be obtained from the response​
print('fail with server error, code: {}'.format(e.code))​
# request id can be used to locate specific issues, it is strongly recommended to save it in the log.​
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))​
​
Related documentation​
For API documentation on getting object metadata, see HeadObject.​
Last updated: 2025.09.17 18:22:10