Este tópico descreve como usar o método UploadPartCopy no OSS SDK for PHP V2 para copiar várias partes de um objeto de origem para um bucket de destino na mesma região e, em seguida, combiná-las em um objeto completo.
Observações
O código de exemplo neste tópico usa a região China (Hangzhou) (
cn-hangzhou) como exemplo. Por padrão, você acessa os recursos de um bucket por um endpoint público. Para acessar recursos no bucket a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre as regiões e os endpoints compatíveis com o OSS, consulte OSS regions and endpoints.Para copiar um objeto, você deve ter permissão de leitura no objeto de origem e permissões de leitura e gravação no bucket de destino.
O OSS não oferece suporte a cópias entre regiões. Por exemplo, você não pode copiar objetos de um bucket na região China (Hangzhou) para um bucket na região China (Qingdao).
Ao copiar um objeto, verifique se não há políticas de retenção configuradas para o bucket de origem ou de destino. Caso contrário, o sistema retornará o erro The object you specified is immutable..
Este tópico fornece um exemplo de como obter credenciais de acesso de uma variável de ambiente. Para mais exemplos de como configurar credenciais de acesso, consulte Configure access credentials for PHP.
Código de exemplo
Use o código a seguir para copiar várias partes de um objeto de origem para um bucket de destino e, em seguida, combiná-las em um objeto completo.
<?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.
}
Referências
Para o código de exemplo completo de cópia multipart, consulte Exemplo no GitHub.