• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (PHP SDK)
  4. Object Upload
  5. Performing a Multipart Copy

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. Sample code is as follows:

// Import the third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [
    'key' => '*** Provide your Access Key ***',
    'secret' => '*** Provide your Secret Key ***',
    'endpoint' => 'https://yourdomainname'
] );

$destBucketName = 'destbucketname';
$destObjectKey = 'destobjectkey';
$sourceBucketName = 'sourcebucketname';
$sourceObjectKey = 'sourceobjectkey';

// Initialize 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']);
NOTE:

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.