Object metadata describes an object stored in OSS. Each object has two categories of metadata: system-defined metadata and user-defined metadata.
System-defined metadata: Standard HTTP headers set by OSS or by you at upload time, such as
Content-Type,Content-Disposition,Cache-Control, andExpires. Some fields are read-only (for example,Content-Length,ETag, andLast-Modified); others are writable.User-defined metadata: Custom key-value pairs you attach to an object, prefixed with
x-oss-meta-in the underlying API. Use these to store application-specific attributes alongside the object.
This topic shows how to set, query, and modify object metadata using OSS SDK for Python V2.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket and the object key of the target object
The
oss:PutObjectpermission to set metadata, or theoss:GetObjectpermission to retrieve metadata. For details, see Attach a custom policy to a RAM userAccess credentials configured as environment variables. For details, see Configure access credentials
The examples in this topic use the region ID cn-hangzhou. By default, a public endpoint is used. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint. For a full list of regions and endpoints, see Regions and endpoints.Query object metadata
OSS provides two methods to retrieve metadata, depending on how much information you need.
| Method | Returns | Use when |
|---|---|---|
HeadObject | All metadata (system-defined and user-defined) | You need the full metadata set |
GetObjectMeta | Six fields only: Content-Length, ETag, Last-Modified, Last-Access-Time, version ID, and CRC-64 hash | You need a lightweight check without downloading the object body |
Modify metadata of an existing object
To modify the metadata of an existing object, copy the object back to itself with metadata_directive='REPLACE' and supply the new metadata set.
OSS SDK for Python V2 provides two ways to copy an object with updated metadata:
`CopyObject`: Copies an object and replaces its metadata.
`Copier.Copy`: A higher-level client that handles both single-part and multipart copy automatically. Use this to overwrite all existing metadata with new metadata, remove original metadata, update only specified metadata headers, or optionally delete the source object after the copy completes.