All Products
Search
Document Center

Object Storage Service:Rename a file (PHP SDK V2)

Last Updated:Mar 20, 2026

OSS does not support direct object renaming. To rename an object within the same bucket, use the CopyObject operation to copy the source object to a destination object with a new name.

Usage notes

  • The sample code uses the public endpoint for the China (Hangzhou) region (cn-hangzhou) by default. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint. For a full list of regions and endpoints, see OSS regions and endpoints.

  • All examples use EnvironmentVariableCredentialsProvider to load credentials from environment variables, avoiding hardcoded keys.

Sample code

Rename an object using the simple copy (CopyObject) method

Use CopyObject to rename an object within the same bucket.

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Load credentials from environment variables (OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET).
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion('<region>');           // e.g., cn-hangzhou
// $cfg->setEndpoint('<endpoint>');    // Optional: override the default endpoint

$client = new Oss\Client($cfg);

// Copy the source object to a new key in the same bucket.
$request = new Oss\Models\CopyObjectRequest(
    bucket: '<bucket-name>',
    key: '<destination-key>',          // The new object name
    sourceKey: '<source-key>',         // The original object name
    sourceBucket: '<bucket-name>'      // Same bucket for renaming; use a different value for cross-bucket copy
);

$result = $client->copyObject($request);

printf(
    'status code: %s' . PHP_EOL .
    'request ID: %s' . PHP_EOL,
    $result->statusCode,
    $result->requestId
);

Replace the following placeholders with actual values:

PlaceholderDescriptionExample
<region>Region ID of the bucketcn-hangzhou
<bucket-name>Name of the bucketmy-bucket
<destination-key>New object name (rename target)new-folder/image.jpg
<source-key>Original object nameold-folder/image.jpg

Rename an object using the copy manager (Copier)

The following code shows how to use the Copier to rename an object.

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Load credentials from environment variables (OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET).
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion('<region>');           // e.g., cn-hangzhou
// $cfg->setEndpoint('<endpoint>');    // Optional: override the default endpoint

$client = new Oss\Client($cfg);

// Create a Copier instance from the client.
$copier = $client->newCopier();

$request = new Oss\Models\CopyObjectRequest(
    bucket: '<bucket-name>',
    key: '<destination-key>',          // The new object name
    sourceBucket: '<bucket-name>',
    sourceKey: '<source-key>'          // The original object name
);

$result = $copier->copy(request: $request);

printf(
    'status code: %s' . PHP_EOL .
    'request ID: %s' . PHP_EOL,
    $result->statusCode,
    $result->requestId
);

Replace the placeholders using the same values described in the table above.

What's next

  • Delete objects — delete the source object after a successful copy to complete the rename

  • Copy objects — more options for cross-bucket and cross-region copies