This topic describes how to use the UploadPartCopy method in the OSS SDK for PHP V2 to copy multiple parts from a source object to a destination bucket in the same region, and then combine the parts into a complete object.
Notes
The sample code in this topic uses the China (Hangzhou) region (
cn-hangzhou) as an example. By default, a public endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud services in the same region, you can 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 permission on the source object and read and write permissions on the destination bucket.
Cross-region copy is not supported. For example, you cannot copy objects from a bucket in the China (Hangzhou) region to a bucket in the China (Qingdao) region.
When you copy an object, make sure that no retention policies are configured for the source or destination bucket. Otherwise, the The object you specified is immutable. error is reported.
This topic provides an example of how to obtain access credentials from an environment variable. For more examples of how to configure access credentials, see Configure access credentials for PHP.
Sample code
You can use the following code to copy multiple parts from a source object to a destination bucket and then combine them into a complete object.
<?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 for command-line arguments.
$optsdesc = [
"region" => ['help' => 'The region in which the bucket is located.', 'required' => True], // The region where 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 argument descriptions to the long option format required by getopt.
// Add a colon ":" after each argument to indicate that the argument requires a value.
$longopts = \array_map(function ($key) {
return "$key:";
}, array_keys($optsdesc));
// Parse the command-line arguments.
$options = getopt("", $longopts);
// Verify that required arguments exist.
foreach ($optsdesc as $key => $value) {
if ($value['required'] === True && empty($options[$key])) {
$help = $value['help']; // Obtain the help information for the argument.
echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
exit(1); // If a required argument is missing, exit the program.
}
}
// Extract values from the parsed arguments.
$region = $options["region"]; // The region where 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 credential information from environment variables.
// Use EnvironmentVariableCredentialsProvider to read the Access Key ID and Access Key Secret from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();
// Use the default configurations of the SDK.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider); // Set the credentials provider.
$cfg->setRegion($region); // Set the region where the bucket is located.
if (isset($options["endpoint"])) {
$cfg->setEndpoint($options["endpoint"]); // If an endpoint is provided, set the endpoint.
}
// Set the default endpoint.
$cfg->setEndpoint('http://oss-cn-hangzhou.aliyuncs.com');
// Create an OSS client instance.
$client = new Oss\Client($cfg);
// Initialize the multipart upload task.
$initRequest = new Oss\Models\InitiateMultipartUploadRequest(bucket: $bucket, key: $key);
$initResult = $client->initiateMultipartUpload($initRequest);
// Determine the name of the source bucket.
if (!empty($options["src-bucket"])) {
$sourceBucket = $options["src-bucket"];
} else {
$sourceBucket = $bucket;
}
// Obtain the metadata of the source object.
$headResult = $client->headObject(new Oss\Models\HeadObjectRequest(
bucket: $sourceBucket,
key: $srcKey
));
// Obtain the size of the source object.
$length = $headResult->contentLength;
// Define the part size and calculate the number of parts.
$partSize = 64 * 1024 * 1024; // The part size in bytes. In this example, the part size is set to 64 MB.
$partsNum = intdiv(num1: $length, num2: $partSize) + intval(1); // Calculate the number of parts and round up.
// Initialize the part list.
$parts = array();
// Traverse each part and perform the part copy operation.
for ($i = 1; $i <= $partsNum; $i++) {
$partRequest = new Oss\Models\UploadPartCopyRequest(
bucket: $bucket,
key: $key,
partNumber: $i,
uploadId: $initResult->uploadId
);
// Set the range of the part.
$partRequest->sourceRange = getPartRange(totalSize: $length, partSize: $partSize, partNumber: $i);
// Set the name of the source bucket (if a source bucket is provided).
if (!empty($options["src-bucket"])) {
$partRequest->sourceBucket = $options["src-bucket"];
}
// Set the name of the source object.
$partRequest->sourceKey = $srcKey;
// Perform the part copy operation.
$partResult = $client->uploadPartCopy($partRequest);
// Create an UploadPart object and add it to the part array.
$part = new Oss\Models\UploadPart(
partNumber: $i,
etag: $partResult->etag,
);
array_push($parts, $part);
}
// Complete the multipart upload task.
$comResult = $client->completeMultipartUpload(
new Oss\Models\CompleteMultipartUploadRequest(
bucket: $bucket,
key: $key,
uploadId: $initResult->uploadId,
completeMultipartUpload: new Oss\Models\CompleteMultipartUpload(
parts: $parts
),
)
);
// Print the result of the completed multipart upload.
printf(
'status code:' . $comResult->statusCode . PHP_EOL . // The HTTP status code. For example, 200 indicates success.
'request id:' . $comResult->requestId . PHP_EOL . // The request ID, which is used to debug or trace the request.
'result:' . var_export($comResult, true) . PHP_EOL // The detailed result of the completed multipart upload.
);
/**
* Obtain the byte range of a part.
*
* @param int $totalSize The total size of the object.
* @param int $partSize The part size.
* @param int $partNumber The number of the current part.
* @return string The byte range string of the part.
*/
function getPartRange(int $totalSize, int $partSize, int $partNumber): string
{
$start = ($partNumber - 1) * $partSize; // Calculate the start position of the part.
$end = min($partNumber * $partSize - 1, $totalSize - 1); // Calculate the end position of the part.
return sprintf('bytes %d-%d', $start, $end); // Format the string as a byte range.
}
References
For the complete sample code for multipart copy, see Github sample.