Updated on 2026-01-16 GMT+08:00

Performing a Multipart Copy

As a special case of multipart upload, multipart copy implements multipart upload by copying the whole or partial object in a bucket.

You can call ObsClient->copyPart to copy parts.

This example copies object sourceobjectname from bucket sourcebucketname to bucket destbucketname as object destobjectname.

The example code is as follows:

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during source code installation.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
      //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
      //Obtain an AK/SK pair on the management console.
      'key' => getenv('ACCESS_KEY_ID'),
      'secret' => getenv('SECRET_ACCESS_KEY'),
      'endpoint' => 'https://your-endpoint'
] );

$destBucketName = 'destbucketname';
$destObjectKey = 'destobjectname';
$sourceBucketName = 'sourcebucketname';
$sourceObjectKey = 'sourceobjectname';

// Initiate a multipart upload.
$resp = $obsClient->initiateMultipartUpload ( [ 
      'Bucket' => $destBucketName,
      'Key' => $destObjectKey
] );
$uploadId = $resp ['UploadId'];
printf ( "UploadId:%s\n\n", $uploadId );

// Obtain information about the large object.
$resp = $obsClient->getObjectMetadata ( [ 
      'Bucket' => $sourceBucketName,
      'Key' => $sourceObjectKey 
]);

// Set the part size to 100 MB.
$partSize = 100 * 1024 * 1024;

$objectSize = $resp ['ContentLength'];

// Calculate the number of parts to be copied.
$partCount = $objectSize % $partSize === 0 ? intval ( $objectSize / $partSize ) : intval ( $objectSize / $partSize ) + 1;

// Start copying parts concurrently.
$promise = null;
$parts = [];
for($i = 0; $i < $partCount; $i ++) {
       $rangeStart = $i * $partSize;
       $rangeEnd = ($i + 1 === $partCount) ? $objectSize - 1 : $rangeStart + $partSize - 1;
       $partNumber = $i + 1;
       $p = $obsClient->copyPartAsync ( [ 
               'Bucket' => $destBucketName,
               'Key' => $destObjectKey,
               'UploadId' => $uploadId,
               'PartNumber' => $partNumber,
               'CopySource' => sprintf ( '%s/%s', $sourceBucketName, $sourceObjectKey ),
               'CopySourceRange' => sprintf ( 'bytes=%d-%d', $rangeStart, $rangeEnd ) 
       ], function ($exception, $resp) use (&$parts, $partNumber) {
              $parts [] = [ 
                           'PartNumber' => $partNumber,
                           'ETag' => $resp ['ETag'] 
              ];
              printf ( "Part#" . strval ( $partNumber ) . " done\n\n" );
       } );
       
       if ($promise === null) {
              $promise = $p;
       }
}

// Wait until the copy is complete.
$promise->wait ();

usort ( $parts, function ($a, $b) {
       if ($a ['PartNumber'] === $b ['PartNumber']) {
              return 0;
       }
       return $a ['PartNumber'] > $b ['PartNumber'] ? 1 : - 1;
} );


// Combine parts.
$resp = $obsClient->completeMultipartUpload ( [ 
        'Bucket' => $destBucketName,
        'Key' => $destObjectKey,
        'UploadId' => $uploadId,
        'Parts' => $parts 
] );
       
printf("Complete to upload multiparts finished, RequestId:%s\n", $resp['RequestId']);

Use the PartNumber parameter to specify the part number, the UploadId parameter to specify the globally unique ID for the multipart upload, the CopySource parameter to specify the information about the source object, and the CopySourceRange parameter to specify the copy range.