Modify the metadata of an existing object (PHP SDK V1)

Updated at:

This topic describes how to modify the metadata of an existing object.

Notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.

  • This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Create an OssClient.

  • To modify the metadata of an existing object, you must have the oss:GetObject and oss:PutObject permissions. For more information, see Grant custom permissions to a RAM user.

Sample code

The following code provides an example of how to modify the metadata of an existing object.

<?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\CoreOssException;

// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// The China (Hangzhou) endpoint is used as an example. Replace it with the actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the name of the source bucket. Example: srcexamplebucket.
$fromBucket = "srcexamplebucket";
// Specify the full path of the source object. The full path cannot contain the bucket name. Example: srcdir/exampleobject.txt.
$fromObject = "<yourFromObjectName>";
// Specify the name of the destination bucket that is in the same region as the source bucket. Example: destexamplebucket.
$toBucket = "destexamplebucket";
// Specify the full path of the destination object. The full path cannot contain the bucket name. Example: destdir/exampleobject.txt.
$toObject = "destdir/exampleobject.txt"; 
$copyOptions = array(
    OssClient::OSS_HEADERS => array(
        'Expires' => '2018-10-01 08:00:00',
        'Content-Disposition' => 'attachment; filename="xxxxxx"',
        'x-oss-meta-location' => 'location',
        // Specify how to set the metadata of the destination object. In this example, this parameter is set to 'REPLACE'. This value indicates that the metadata of the source object is ignored and the metadata specified in the request is used. If you set this parameter to COPY, the metadata of the source object is copied to the destination object.
        '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);
    // Modify the metadata of the existing object.
    $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $copyOptions);
} catch(OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}
print(__FUNCTION__ . ": OK" . "\n");            

References

  • For the complete sample code for modifying the metadata of an existing object, see GitHub.

  • For more information about the API operation to modify the metadata of an existing object, see CopyObject.