Object metadata includes HTTP headers and user metadata.

Note For more information about object metadata, see Manage object metadata in OSS Developer Guide.

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
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Specify the endpoint of 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. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')
# 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 = '{"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 the user metadata of an object to describe the object.

Note The user metadata is prefixed with x-oss-meta- in OSS.

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

# -*- coding: utf-8 -*-
import oss2

# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Specify the endpoint of 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. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')
# 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 that must be prefixed with x-oss-meta-. For example, set the x-oss-meta-author metadata to '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

# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Specify the endpoint of 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. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')
# Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path cannot contain the bucket name. 
object_name = 'exampledir/exampleobject.txt'

bucket.update_object_meta(object_name, {'x-oss-meta-author': 'O. Henry'})
# Each time you call bucket.update_object_meta, the existing user metadata is updated. 
bucket.update_object_meta(object_name, {'x-oss-meta-price': '100 dollar'})

The following code provides an example on how to modify object metadata such as Content-Type:

# -*- coding: utf-8 -*-
import oss2

# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Specify the endpoint of 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. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')
# Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path cannot contain the bucket name. 
object_name = 'exampledir/exampleobject.txt'

bucket.update_object_meta(object_name, {'x-oss-meta-author': 'O. Henry'})
# Each time you call bucket.update_object_meta, the existing user metadata is updated. 
bucket.update_object_meta(object_name, {'Content-Type': 'text/plain'})

Obtain object metadata

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

Method Description Advantage
get_object_meta Obtains part of object metadata such as the ETag, Size, and LastModified (the last modified time) values of the object. More lightweight and faster
head_object Obtains all object metadata. N/A

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

# -*- coding: utf-8 -*-
import oss2

# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Specify the endpoint of 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. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')
# Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path cannot contain the bucket name. 
object_name = 'exampledir/exampleobject.txt'

# Obtain part of the object metadata by using the get_object_meta method. 
simplifiedmeta = bucket.get_object_meta(object_name)
print(simplifiedmeta.headers['Last-Modified']) 
print(simplifiedmeta.headers['Content-Length']) 
print(simplifiedmeta.headers['ETag']) 

# Obtain all object metadata by using the head_object method. 
objectmeta = bucket.head_object(object_name)
# In this example, the returned part of the object metadata is displayed. You can add the code to display all object metadata. 
print(objectmeta.headers['Content-Type']) 
print(objectmeta.headers['Last-Modified']) 
print(objectmeta.headers['x-oss-object-type'])

References

  • For the complete sample code that is used to configure and obtain object metadata, visit GitHub.
  • For more information about the API operation that you can call to configure object metadata, see PutObject.
  • For more information about the API operation that you can call to obtain object metadata, see GetObjectMeta.