All Products
Search
Document Center

Object Storage Service:Modify the metadata of an existing object (PHP SDK V1)

Last Updated:Mar 20, 2026

OSS does not support modifying object metadata in place. To update the metadata of an existing object, copy the object to itself using copyObject() and specify the new metadata in the request. The original object data is preserved; only the metadata changes.

Prerequisites

Before you begin, ensure that you have:

  • oss:GetObject permission on the source object

  • oss:PutObject permission on the destination bucket

How it works

copyObject() copies an object from a source location to a destination location. To modify metadata without moving the object, set the source and destination to the same bucket and object path.

The x-oss-metadata-directive header controls what happens to the metadata during the copy:

ValueBehavior
REPLACEIgnores the source object's metadata. Uses only the metadata headers specified in the request.
COPYCopies all metadata from the source object to the destination. Metadata headers in the request are ignored.

Set x-oss-metadata-directive to REPLACE when updating metadata.

Modify object metadata

The following example copies an object to itself and replaces its metadata.

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;

// Load credentials from environment variables.
// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
$provider = new EnvironmentVariableCredentialsProvider();

// Replace with your bucket's endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";

// The source and destination are the same object.
$bucket = "srcexamplebucket";
$object = "srcdir/exampleobject.txt";

// Specify the new metadata. Set x-oss-metadata-directive to REPLACE
// so that only the headers in this request are applied to the object.
$copyOptions = array(
    OssClient::OSS_HEADERS => array(
        'Expires'                  => '2018-10-01 08:00:00',
        'Content-Disposition'      => 'attachment; filename="xxxxxx"',
        'x-oss-meta-location'      => 'location',
        'x-oss-metadata-directive' => 'REPLACE',
    ),
);

try {
    $config = array(
        "provider"         => $provider,
        "endpoint"         => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"           => "cn-hangzhou",
    );
    $ossClient = new OssClient($config);

    // Copy the object to itself with the new metadata.
    $ossClient->copyObject($bucket, $object, $bucket, $object, $copyOptions);
} catch (OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}
print(__FUNCTION__ . ": OK\n");

Replace the following placeholders with your actual values:

PlaceholderDescriptionExample
https://oss-cn-hangzhou.aliyuncs.comEndpoint for the region where your bucket is located. Use an internal endpoint when accessing OSS from another Alibaba Cloud service in the same region.https://oss-cn-beijing.aliyuncs.com
srcexamplebucketName of your bucketmy-bucket
srcdir/exampleobject.txtFull path of the object (excluding the bucket name)images/photo.jpg

What's next