Object metadata is a set of key-value pairs attached to an OSS object. OSS supports two categories of metadata: standard HTTP headers (such as Content-Type, Expires, and Cache-Control) and custom metadata (arbitrary key-value pairs with keys prefixed with x-oss-meta-). This document shows how to set metadata during upload, retrieve it, and update it on existing objects.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket in a known region
oss:PutObjectpermission to set object metadataoss:GetObjectpermission to retrieve object metadataAccess credentials configured as environment variables (see Configure access credentials)
Usage notes
The sample code uses the China (Hangzhou) region (
cn-hangzhou) and a public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For region-to-endpoint mappings, see Regions and endpoints.For RAM (Resource Access Management) permission details, see Grant custom permissions to a RAM user.
Supported metadata fields
The following fields can be set on an object when uploading or copying. Standard HTTP headers are returned in the response when the object is downloaded or queried via HeadObject.
| Field | Type | Description |
|---|---|---|
Expires | string (RFC 1123) | Expiration time of the object. Pass a string formatted with time.Time.Format(time.RFC1123). |
Acl | oss.ObjectACLType | Access control list (ACL) for the object, for example, oss.ObjectACLPublicRead. |
Metadata | map[string]string | Custom key-value pairs. Keys must be prefixed with x-oss-meta-. |
Retrieve object metadata
OSS provides two methods to retrieve object metadata. Choose based on how much information you need:
| Method | Returns | Use when |
|---|---|---|
HeadObject | All metadata, including custom metadata and standard HTTP headers | You need custom metadata or the full set of headers |
GetObjectMeta | Partial metadata only: ContentLength, ETag, LastModified, LastAccessTime, VersionId, and HashCRC64 | You need a lightweight check without retrieving the full header set |
Update metadata on an existing object
OSS does not provide a dedicated metadata update operation. To update metadata on an existing object, copy the object and set MetadataDirective to "Replace". This replaces all existing metadata with the values you specify in the copy request; the original metadata is discarded.
Two copy methods are available:
| Method | Best for |
|---|---|
CopyObject | Standard copy operation |
Copier.Copy (copy manager) | Large objects; handles multipart copy automatically |