Tous les produits
Search
Centre de documentation

Object Storage Service:Copie multipart (PHP SDK V2)

Dernière mise à jour :Sep 25, 2026

Cette rubrique explique comment utiliser la méthode UploadPartCopy du SDK OSS pour PHP V2 afin de copier plusieurs parties d'un objet source vers un bucket de destination situé dans la même région, puis d'assembler ces parties en un objet complet.

Notes

  • Le code exemple de cette rubrique utilise la région Chine (Hangzhou) (cn-hangzhou) à titre d'exemple. Par défaut, un endpoint public est utilisé pour accéder aux ressources d'un bucket. Pour accéder aux ressources du bucket depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints pris en charge par OSS, consultez la page Régions et endpoints OSS.

  • Pour copier un objet, vous devez disposer des autorisations de lecture sur l'objet source et des autorisations de lecture et d'écriture sur le bucket de destination.

  • La copie interrégionale n'est pas prise en charge. Par exemple, vous ne pouvez pas copier des objets d'un bucket situé dans la région Chine (Hangzhou) vers un bucket situé dans la région Chine (Qingdao).

  • Lorsque vous copiez un objet, assurez-vous qu'aucune politique de rétention n'est configurée pour le bucket source ou le bucket de destination. Dans le cas contraire, l'erreur The object you specified is immutable. s'affiche.

  • Cette rubrique fournit un exemple montrant comment obtenir des identifiants d'accès à partir d'une variable d'environnement. Pour d'autres exemples de configuration des identifiants d'accès, consultez la page Configurer les identifiants d'accès pour PHP.

Exemple de code

Utilisez le code suivant pour copier plusieurs parties d'un objet source vers un bucket de destination, puis les assembler en un objet complet.

<?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.
}

Références

  • Pour consulter l'exemple de code complet relatif à la copie multipart, rendez-vous sur la page Exemple Github.