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:GetObjectpermission on the source objectoss:PutObjectpermission 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:
| Value | Behavior |
|---|---|
REPLACE | Ignores the source object's metadata. Uses only the metadata headers specified in the request. |
COPY | Copies 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:
| Placeholder | Description | Example |
|---|---|---|
https://oss-cn-hangzhou.aliyuncs.com | Endpoint 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 |
srcexamplebucket | Name of your bucket | my-bucket |
srcdir/exampleobject.txt | Full path of the object (excluding the bucket name) | images/photo.jpg |
What's next
For the complete sample code, see GitHub.
For the underlying API operation, see CopyObject.
To create an OssClient instance using a custom domain name or Security Token Service (STS), see Create an OSSClient instance.
For all supported regions and endpoints, see Regions and endpoints.
To grant the required permissions to a RAM user, see Grant custom permissions to a RAM user.