Help Center> Object Storage Service> Node.js> Object Upload> Performing a Multipart Copy
Updated on 2023-11-09 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 the 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. Sample code is as follows:

// Import the OBS library.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use source codes to install the client.
// var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var 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. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

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

// 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');
                                         }
                                  });
                           });
                     }
              });
       }
});

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.