• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Node.js 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 OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

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

// Initialize a multipart upload.
obsClient.initiateMultipartUpload({
       Bucket : destBucketName,
       Key : destObjectKey
}, (err, result) => {
       if(!err && result.CommonMsg.Status < 300){
              var uploadId = result.InterfaceResult.UploadId;
              console.log('\t' + uploadId + '\n');
              
              // Obtain information about the large object.
              obsClient.getObjectMetadata({
                     Bucket : sourceBucketName,
                     Key : sourceObjectKey
              }, (err, result) => {
                     if(!err && result.CommonMsg.Status < 300){
                           // Set the part size to 100 MB.
                           var partSize = 100 * 1024 * 1024;
                           var objectSize = Number(result.InterfaceResult.ContentLength);
                           
                           // Calculate the number of parts to be copied.
                           var partCount = objectSize % partSize === 0 ? Math.floor(objectSize / partSize) : Math.floor(objectSize / partSize) + 1;
                           
                           var events = require('events');
                           var eventEmitter = new events.EventEmitter();
                           var parts = [];
                           
                           // Concurrently copy parts. 
                           for(let i=0;i<partCount;i++){
                                  let rangeStart = i * partSize;
                                  let rangeEnd = (i + 1) === partCount ? objectSize - 1 : rangeStart + partSize - 1;
                                  let partNumber = i + 1;
                                  obsClient.copyPart({
                                         Bucket: destBucketName,
                                         Key: destObjectKey,
                                         PartNumber : partNumber,
                                         UploadId : uploadId,
                                         CopySource: sourceBucketName + '/' + sourceObjectKey,
                                         CopySourceRange : 'bytes=' + rangeStart + '-' + rangeEnd
                                  }, (err, result) => {
                                         if(!err && result.CommonMsg.Status < 300){
                                                parts.push({PartNumber : partNumber, ETag : result.InterfaceResult.ETag});
                                                if(parts.length === partCount){
                                                       var _parts = parts.sort((a, b) => {
                                                              if(a.PartNumber >= b.PartNumber){
                                                                     return 1;
                                                              }
                                                              return -1;
                                                       });
                                                       eventEmitter.emit('copy part finished', _parts);
                                                }
                                         }else{
                                                throw new Error(err || result.CommonMsg.Code);
                                         }
                                  });
                           }
                           
                           // Wait until the copy is complete.
                           eventEmitter.on('copy part finished', (parts) => {
                                  // Combine the parts.
                                  obsClient.completeMultipartUpload({
                                         Bucket: destBucketName,
                                         Key: destObjectKey,
                                         UploadId: uploadId,
                                         Parts: parts
                                  }, (err, result) => {
                                         if(!err && result.CommonMsg.Status < 300){
                                                console.log('Complete to upload multiparts finished.\n');
                                         }
                                  });
                           });
                     }
              });
       }
});
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.