Updated on 2026-09-22 GMT+08:00

Performing a Multipart Copy

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see API Reference.

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 in multipart mode.

The sample code is as follows:

<?php

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
use Obs\ObsException;
use GuzzleHttp\Promise\Utils;
// Create an ObsClient instance.
$obsClient = new ObsClient([
    // Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
    // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
    '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;
// Copy parts concurrently.
$promises = [];
$parts = [];
for($i = 0; $i < $partCount; $i ++) {
    $rangeStart = $i * $partSize;
    $rangeEnd = ($i + 1 === $partCount) ? $objectSize - 1 : $rangeStart + $partSize - 1;
    $partNumber = $i + 1;
    $promises[] = $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) {
        if ($exception !== null) {
            printf( "Part#" . strval ( $partNumber ) . " failed: " . $exception->getMessage () . "\n\n" );
            return;
        }
        $parts [] = [
            'PartNumber' => $partNumber,
            'ETag' => $resp['ETag']
        ];
        printf("Part#%d done\n\n", $partNumber);
    });
}
// Wait until all parts are copied.
Utils::all( $promises )->wait ();
usort ( $parts, function ($a, $b) {
    if ($a ['PartNumber'] === $b ['PartNumber']) {
        return 0;
    }
    return $a ['PartNumber'] > $b ['PartNumber'] ? 1 : - 1;
});
try {
    // Assemble parts.
    $resp = $obsClient->completeMultipartUpload([
        'Bucket' => $destBucketName,
        'Key' => $destObjectKey,
        'UploadId' => $uploadId,
        'Parts' => $parts
    ]);;
  printf("Complete to upload multiparts finished, RequestId:%s\n", $resp['RequestId']);
} catch (ObsException $e) {
    // If the operation fails, obtain the exception details.
    printf("InitiateMultipartUploadFailed: Status:%d, Code:%s, Message:%s\n", $e->getStatusCode(), $e->getExceptionCode(), $e->getExceptionMessage());
} finally {
    $obsClient->close();
}

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.