All Products
Search
Document Center

Object Storage Service:Manage object metadata

Last Updated:Apr 03, 2025

Objects stored in Object Storage Service (OSS) consist of keys, data, and object metadata. Object metadata describes object attributes. Object metadata includes standard HTTP headers and user metadata. You can create custom HTTP request policies such as object cache policies and forced object download policies by configuring standard HTTP headers. You can also configure user metadata for an object to identify the purposes or attributes of the object.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

  • To configure object metadata, you must have the oss:PutObject permission. To query object metadata, you must have the oss:GetObject permission. For more information, see Attach a custom policy to a RAM user.

Configure HTTP headers

The following code provides an example on how to configure HTTP headers for the exampleobject.txt object in the exampledir directory of the examplebucket bucket.

Note

For more information about HTTP headers, see RFC 2616.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint for the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region of the endpoint. Example: cn-hangzhou. This parameter is required if you use the V4 signature algorithm.
region = "cn-hangzhou"

# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path cannot contain the bucket name.
object_name = 'exampledir/exampleobject.txt'
# Specify the string to upload.
content = '{"age": 1}'
# Configure HTTP headers. For example, set the Content-Type header to 'application/json; charset=utf-8'.
bucket.put_object(object_name, content, headers={'Content-Type': 'application/json; charset=utf-8'})

Configure user metadata

You can configure user metadata to describe an object.

The following sample code provides an example on how to configure user metadata for the exampleobject.txt object in the exampledir directory of the examplebucket bucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint for the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region of the endpoint. Example: cn-hangzhou. This parameter is required if you use the V4 signature algorithm.
region = "cn-hangzhou"

# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path cannot contain the bucket name.
object_name = 'exampledir/exampleobject.txt'
# Specify the string that you want to upload.
content = 'a novel'

# Configure the user metadata. User metadata is configured by specifying custom headers prefixed with x-oss-meta-. Sample header: x-oss-meta-author. Sample value: O. Henry.
bucket.put_object(object_name, content, headers={'x-oss-meta-author': 'O. Henry', 'Content-Type': 'application/json; charset=utf-8'})

Modify object metadata

The following code provides an example on how to modify the metadata of the exampleobject.txt object in the exampledir directory of the examplebucket bucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint for the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region of the endpoint. Example: cn-hangzhou. This parameter is required if you use the V4 signature algorithm.
region = "cn-hangzhou"

# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path cannot contain the bucket name.
object_name = 'exampledir/exampleobject.txt'
# Modify the object metadata.
bucket.update_object_meta(object_name, {'x-oss-meta-author': 'O. Henry'})
# Each time you call bucket.update_object_meta, the user metadata is updated.
bucket.update_object_meta(object_name, {'Content-Type': 'text/plain'})

Query object metadata

You can use the methods provided by OSS SDK for Python to query object metadata.

Method

Description

Advantage

get_object_meta

Queries part of the object metadata, including ETag, Content-Length, LastModified, and CRC64-ECMA.

Lightweight and fast. Suitable for scenarios where only basic information is required.

head_object

Queries all object metadata, including Content-Length, Content-Type, storage-class, Content-MD5, and CRC64-ECMA.

More comprehensive. Suitable for scenarios where complete object information is required.

The following code provides an example on how to query the metadata of the exampleobject.txt object in the exampledir directory of the examplebucket bucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint for the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region of the endpoint. Example: cn-hangzhou. This parameter is required if you use the V4 signature algorithm.
region = "cn-hangzhou"

# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path cannot contain the bucket name.
object_name = 'exampledir/exampleobject.txt'

# Query part of the object metadata by using the get_object_meta method.
simplifiedmeta = bucket.get_object_meta(object_name)
# Display part of the object metadata
print("Last-Modified: " + simplifiedmeta.headers['Last-Modified'])  # Query the last modified time of the object
print("Content-Length: " + simplifiedmeta.headers['Content-Length'])  # Query the size of the object
print("ETag: " + simplifiedmeta.headers['ETag'])  # Query the ETag value of the object
# Query the object metadata, including the last access time of the object, after you enable access tracking. You can use only OSS SDK for Python 2.16.1 and later to query the last access time of objects.
# print(simplifiedmeta.headers['x-oss-last-access-time'])

# Query all object metadata by using the head_object method.
objectmeta = bucket.head_object(object_name)
# In this example, only part of the object metadata is displayed. You can add code lines to display other object metadata.
# Display part of the object metadata
print("Content-Type: " + objectmeta.headers['Content-Type'])  # Query the MIME type of the object
print("Content-MD5: " + objectmeta.headers['Content-MD5'])  # Query the MD5 hash value of the object
print("x-oss-storage-class: " + objectmeta.headers['x-oss-storage-class'])  # Query the storage class of the object
print("x-oss-hash-crc64ecma: " + objectmeta.headers['x-oss-hash-crc64ecma'])  # Query the CRC64-ECMA hash value of the object


# Display all header fields
# To display all header fields, uncomment the following code
# print("\nAll headers:")
# for key, value in objectmeta.headers.items():
#     print(f"{key}: {value}")

References

  • For more information about object metadata, see Manage object metadata.

  • For more information about the API operation that you can call to configure object metadata during simple upload, see PutObject.

  • For more information about the API operations that you can call to query object metadata, see GetObjectMeta and HeadObject.