All Products
Search
Document Center

Object Storage Service:Resumable upload (Node.js SDK)

Last Updated:Nov 29, 2025

Resumable upload divides a file into multiple parts. These parts are uploaded separately and then merged to form the complete file.

Note

The catch syntax in the following sample code is based on ES6 Promise and async/await. For more information about how to use the software development kit (SDK), see Install the Node.js SDK.

For more information about resumable upload, see Resumable upload. You can also set lifecycle rules to periodically delete unwanted fragments. For more information, see Delete fragments.

The multipart upload feature provides the progress parameter that you can use to specify a progress callback. In the callback, the SDK passes the completion percentage and breakpoint information as parameters. To perform a resumable upload, save the checkpoint information during the upload. If an error occurs, pass the saved checkpoint to the `multipartUpload` function. The upload then resumes from where it failed.

Sample code

The following code shows how to perform a resumable upload.

const OSS = require('ali-oss')

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'examplebucket',
});

// Set yourfilepath to the local path of the file to upload.
const filePath = "yourfilepath";

let checkpoint;
async function resumeUpload() {
  // Retry five 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 the current loop.
    } catch (e) {
      console.log(e);
    }
  }
}

resumeUpload();

The preceding sample code saves the checkpoint to a variable. If the program crashes, the checkpoint information is lost. To prevent this, save the checkpoint to a file. This allows the program to read the checkpoint information from the file after a restart.

References

For the complete sample code for resumable upload, see the GitHub example.