Objects cannot be directly renamed. To rename an object in the bucket, you can call the CopyObject operation to copy the source object and rename the new object. Then, call the DeleteObject operation to delete the source object.

Examples

The following code provides examples on how to rename an object named srcobject.txt in the examplebucket bucket to destobject.txt:

<?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\OssClient;
use OSS\Core\OssException;

// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in Object Storage Service (OSS) is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name. Example: examplebucket. 
$bucket= "examplebucket";
// Specify the full path of the source object. The full path of the object cannot contain the bucket name. Example: srcobject.txt. 
$fromObject = "srcobject.txt";
// Specify the full path of the destination object. The full path of the object cannot contain the bucket name. Example: destobject.txt. 
$toObject = 'destobject.txt';

try {
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    // Copy the srcobject.txt object in examplebucket to the destobject.txt object in the same bucket. 
    $ossClient->copyObject($bucket, $fromObject,$bucket, $toObject);
    // Delete the srcobject.txt object. 
    $ossClient->deleteObject($bucket, $fromObject);
} catch (OssException $e) {
    printf($e->getMessage() . "\n");
    return;
}
print("Object ".$fromObject ." Rename complete" . PHP_EOL);
Note Directories in a bucket also cannot be directly renamed. To rename a directory in a bucket, you can follow the preceding example to rename the subdirectories and objects in the directory one by one.

References

For more information about the API operations that you can call to rename an object, see CopyObject and DeleteObject.