Object metadata describes the properties of an object stored in OSS. Use the OSS SDK for PHP V2 to set metadata when uploading objects, retrieve metadata without downloading object content, and update metadata on existing objects.
Key concepts
Object metadata falls into two categories:
System metadata: Managed by OSS. Includes standard HTTP headers such as
Content-Type,Content-Length,ETag, andLast-Modified. Some system metadata, such asContent-Type, can be set by users at upload time.User metadata: Custom key-value pairs you define.
Important constraint: OSS does not support in-place metadata updates. To change the metadata of an existing object, copy the object to the same or a different destination with the updated metadata using CopyObject or Copier.Copy, then set metadataDirective to REPLACE.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket in your target region
The
oss:PutObjectpermission to set metadata, or theoss:GetObjectpermission to query metadata. For details, see Attach a custom policy to a RAM userAccess credentials stored in environment variables (
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRET)
Usage notes
The sample code uses
cn-hangzhouas the region. Replace it with your actual region ID.By default, a public endpoint is used. If you're accessing OSS from another Alibaba Cloud service in the same region, use an internal endpoint. For the full list of regions and endpoints, see Regions and endpoints.
Set metadata on upload
Query object metadata
OSS provides two operations for querying metadata without downloading object content:
| Operation | What it returns | Use when |
|---|---|---|
HeadObject | All system and user metadata | You need the full set of object properties |
GetObjectMeta | A lightweight subset of system metadata | You only need size, ETag, or timestamps |
Update metadata on an existing object
Because OSS does not support in-place metadata updates, update metadata by copying the object with metadataDirective set to REPLACE. OSS then writes the new metadata to the destination object, discarding the original metadata.
metadataDirective accepts two values:
| Value | Behavior |
|---|---|
COPY | Copy metadata from the source object (default) |
REPLACE | Replace metadata with the values you specify in the request |