An object can be split into several parts that can be uploaded at the same time. After all parts are uploaded, the parts are combined into a complete object.

Note The sample code in this topic uses the catch method. To obtain the syntax of this method, view the Promise function defined in ECMAScript 6 and the async and await methods of this function. For more information about how to use OSS SDK for Node.js, see Installation.

For more information, see Resumable upload in the Developer Guide. Lifecycle rules can be configured to periodically delete the parts that you no longer need. For more information, see Manage parts.

The progress parameter is provided to obtain the upload progress in callback. The SDK uses the upload progress and the checkpoint information as parameters in the callback requests. In resumable upload, checkpoint information is recorded. When the upload task fails because of errors, you can pass the checkpoint information to multipartUpload as a parameter to continue the upload.

Sample code

The following code provides an example on how to perform resumable upload:

const OSS = require('ali-oss');

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret'
 // Set yourbucketname to the name of your bucket. 
  bucket: 'yourbucketname',
});

let checkpoint;
async function resumeUpload() {
  // retry 5 times
  for (let i = 0; i < 5; i++) {
    try {
      const result = await client.multipartUpload('object-name', filePath, {
        checkpoint,
        async progress(percentage, cpt) {
          checkpoint = cpt;
        },
      });
      console.log(result);
      break; // break if success
    } catch (e) {
      console.log(e);
    }
  }
}

resumeUpload();

In the preceding code, the checkpoint information is recorded as a variable. If the program crashes, the checkpoint information is lost. We recommend that you save the checkpoint information in a file. This way, the program can obtain the checkpoint information from the file after the program restarts.

References

For the complete sample code that is used to perform resumable upload, visit GitHub.