This topic describes how to use the CopyObject method of the OSS SDK for PHP V2 to copy an object that is less than 5 GiB from a source bucket to a destination bucket in the same region. The destination bucket can be the source bucket or a different bucket.
Notes
The sample code in this topic uses
cn-hangzhou, the region ID for China (Hangzhou), as an example. By default, a public endpoint is used. If you want to access OSS from other Alibaba Cloud services in the same region, you must use an internal endpoint. For more information about the regions and endpoints that OSS supports, see OSS regions and endpoints.To copy an object, you must have read permissions on the source object and read and write permissions on the destination bucket.
You cannot copy objects across regions. For example, you cannot copy an object from a bucket in the China (Hangzhou) region to a bucket in the China (Qingdao) region.
When you copy an object, you must make sure that no retention policies are configured for the source and destination buckets. Otherwise, the The object you specified is immutable. error is returned.
The sample code in this topic obtains access credentials from environment variables. For more information, see Configure access credentials.
Sample code
You can use the following code to copy an object that is less than 5 GiB from a source bucket to a destination bucket in the same region.
<?php
// Include the autoload file to ensure that dependency libraries are loaded.
require_once __DIR__ . '/../vendor/autoload.php';
use AlibabaCloud\Oss\V2 as Oss;
// Define the descriptions of command-line parameters.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region in which the bucket is located. (Required)
"endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False], // The endpoint. (Optional)
"bucket" => ['help' => 'The name of the bucket', 'required' => True], // The name of the destination bucket. (Required)
"key" => ['help' => 'The name of the object', 'required' => True], // The name of the destination object. (Required)
"src-bucket" => ['help' => 'The name of the source bucket', 'required' => False], // The name of the source bucket. (Optional)
"src-key" => ['help' => 'The name of the source object', 'required' => True], // The name of the source object. (Required)
];
// Convert the parameter descriptions to the long option format required by getopt.
// A colon (:) is added to the end of each parameter to indicate that the parameter requires a value.
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
// Parse the command-line arguments.
$options = getopt("", $longopts);
// Verify that the required parameters are specified.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help']; // Obtain the help information of the parameter.
echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
exit(1); // If a required parameter is not specified, exit the program.
}
}
// Extract values from the parsed parameters.
$region = $options["region"]; // The region in which the bucket is located.
$bucket = $options["bucket"]; // The name of the destination bucket.
$key = $options["key"]; // The name of the destination object.
$srcKey = $options["src-key"]; // The name of the source object.
// Load the credential information from the environment variables.
// Use EnvironmentVariableCredentialsProvider to read the Access Key ID and Access Key Secret from the environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider); // Set the credential provider.
$cfg->setRegion($region); // Set the region in which the bucket is located.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]); // If an endpoint is provided, set the endpoint.
}
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Create a CopyObjectRequest object to copy the object.
$request = new Oss\Models\CopyObjectRequest(
bucket: $bucket,
key: $key);
if (!empty($options["src-bucket"])) {
$request->sourceBucket = $options["src-bucket"]; // If the name of the source bucket is provided, set sourceBucket.
}
$request->sourceKey = $srcKey; // Set the name of the source object.
// Execute the copy object operation.
$result = $client->copyObject($request);
// Print the copy result.
printf(
'status code:' . $result->statusCode . PHP_EOL . // The HTTP status code. For example, 200 indicates that the operation is successful.
'request id:' . $result->requestId . PHP_EOL // The request ID, which is used to debug or trace the request.
);
References
For the complete sample code used to copy an object, see GitHub example.