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 about resumable upload, see Resumable upload. 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 callbacks. 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 your 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',
});

// Set yourfilepath to the local path of the uploaded object. 
const filePath = "yourfilepath";

let checkpoint;
async function resumeUpload() {
  // Set the number of upload retry operations to 5. 
  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 out of the loop. 
    } 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.