All Products
Search
Document Center

Object Storage Service:Multipart upload (Harmony SDK)

Last Updated:Nov 29, 2025

Object Storage Service (OSS) provides a multipart upload feature that lets you split a large object into multiple parts. After these parts are uploaded, you can call the CompleteMultipartUpload operation to combine them into a complete object.

Usage notes

Multipart upload process

A multipart upload consists of the following three steps:

  1. Initiate the multipart upload.

    You can call the client.initiateMultipartUpload method. OSS returns a globally unique upload ID.

  2. Upload parts.

    You can call the client.uploadPart method to upload the parts.

    Note
    • For a specific upload ID, the part number identifies the relative position of a part in the complete object. If you upload a new part with an existing part number, the existing part is overwritten.

    • OSS returns the MD5 hash of the received part in the ETag header.

    • OSS calculates the MD5 hash of the uploaded data and compares it with the MD5 hash calculated by the SDK. If the two hashes do not match, OSS returns the `InvalidDigest` error code.

  3. Complete the multipart upload.

    After all parts are uploaded, you can call the client.completeMultipartUpload method to combine the parts into a complete object.

Sample code

The following code shows how to split a large local file into multiple parts, upload the parts to a bucket concurrently, and then combine the parts into a complete object.

import Client, { FilePath, RequestError, THarmonyEmptyBodyApiRes } from '@aliyun/oss';
import { fileIo as fs } from '@kit.CoreFileKit';

// Create an OSS client instance.
const client = new Client({
  // Replace with the AccessKey ID from your STS temporary access credential.
  accessKeyId: 'yourAccessKeyId',
  // Replace with the AccessKey secret from your STS temporary access credential.
  accessKeySecret: 'yourAccessKeySecret',
  // Replace with the security token from your STS temporary access credential.
  securityToken: 'yourSecurityToken',
  // Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
  region: 'oss-cn-hangzhou',
});

// Specify the name of the bucket to use. Replace with your actual bucket name.
const bucket = 'yourBucketName';
// Specify the name of the object (file) to upload. Replace with your actual object name.
const key = 'yourObjectName';

/**
 * Upload a file to OSS using multipart upload.
 * Use multipart upload to split a large file into multiple parts and upload them to a specified bucket and key.
 */
const multipartUpload = async () => {
  try {
    // Initiate the multipart upload task and get the uploadId.
    const initRes = await client.initiateMultipartUpload({
      bucket, // Bucket name
      key,    // Object (file) name
    });

    // Get the uploadId from the initialization response. It is used for subsequent part uploads and to complete the upload.
    const uploadId = initRes.data.uploadId;

    // Specify the local file path.
    const filePath = new FilePath('yourFilePath');// Replace with the actual local file path.

    // Get the file metadata, such as the file size.
    const fileStat = await fs.stat(filePath.filePath);

    // Define the size of each part (10 MB).
    const chunkSize = 1024 * 1024 * 10;

    // Calculate the total number of parts.
    let totalParts = Math.ceil(fileStat.size / chunkSize);

    // Current part number.
    let partNumber = 1;

    // Store the Promise objects for all part uploads.
    const waitList: Promise<THarmonyEmptyBodyApiRes>[] = [];

    // Loop to process each part.
    while (partNumber <= totalParts) {
      // Calculate the start position of the current part.
      const offset = (partNumber - 1) * chunkSize;

      // Call the uploadPart method to upload the current part.
      const uploadPromise = client.uploadPart({
        bucket, // Bucket name
        key,    // Object (file) name
        uploadId, // The uploadId returned from initialization.
        partNumber, // Current part number.
        data: filePath, // File path.
        length: Math.min(chunkSize, fileStat.size - offset), // Size of the current part.
        offset, // Start offset of the current part.
      });

      // Add the part upload Promise to the wait list.
      waitList.push(uploadPromise);

      // Increment the part number.
      partNumber++;
    }

    // Wait for all part uploads to complete.
    const uploadResList = await Promise.all(waitList);

    // After all parts are uploaded, call completeMultipartUpload to complete the upload task.
    const completeRes = await client.completeMultipartUpload({
      bucket, // Bucket name
      key,    // Object (file) name
      uploadId, // The uploadId returned from initialization.
      completeAll: true, // Automatically complete all parts.
    });

    // Print the result of the completed multipart upload.
    console.log(JSON.stringify(completeRes));
  } catch (err) {
    // Catch exceptions that occur during the request.
    if (err instanceof RequestError) {
      // If a known type of error occurs, print the error code, error message, request ID, status code, EC code, and other information.
      console.log('code: ', err.code); // Error code.
      console.log('message: ', err.message); // Error message.
      console.log('requestId: ', err.requestId); // Request ID.
      console.log('status: ', err.status); // HTTP status code.
      console.log('ec: ', err.ec); // Error code.
    } else {
      // Print other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
};

// Call the multipartUpload function to perform the multipart upload operation.
multipartUpload();

Common scenarios

Cancel a specific multipart upload event

You can use the client.abortMultipartUpload method to cancel a multipart upload.

import Client, { RequestError } from '@aliyun/oss';

// Create an OSS client instance.
const client = new Client({
  // Replace with the AccessKey ID from your STS temporary access credential.
  accessKeyId: 'yourAccessKeyId',
  // Replace with the AccessKey secret from your STS temporary access credential.
  accessKeySecret: 'yourAccessKeySecret',
  // Replace with the security token from your STS temporary access credential.
  securityToken: 'yourSecurityToken',
  // Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
  region: 'oss-cn-hangzhou',
});

// Specify the name of the bucket to use. Replace with your actual bucket name.
const bucket = 'yourBucketName';
// Specify the name of the object to use. Replace with your actual object name.
const key = 'yourObjectName';

/**
 * Abort a multipart upload task.
 * Use the abortMultipartUpload method to abort a specific multipart upload task.
 */
const abortMultipartUpload = async () => {
  try {
    // Call the abortMultipartUpload method to abort a specific multipart upload task.
    const res = await client.abortMultipartUpload({
      bucket, // Bucket name
      key,    // Object (file) name
      // The uploadId can be obtained from the response of client.initiateMultipartUpload or client.listMultipartUploads.
      uploadId: 'uploadId', // Replace with the actual uploadId.
    });

    // Print the result of the aborted multipart upload.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Catch exceptions that occur during the request.
    if (err instanceof RequestError) {
      // If a known type of error occurs, print the error code, error message, request ID, status code, EC code, and other information.
      console.log('code: ', err.code); // Error code.
      console.log('message: ', err.message); // Error message.
      console.log('requestId: ', err.requestId); // Request ID.
      console.log('status: ', err.status); // HTTP status code.
      console.log('ec: ', err.ec); // Error code.
    } else {
      // Print other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
};

// Call the abortMultipartUpload function to perform the abort multipart upload operation.
abortMultipartUpload();

List successfully uploaded parts in a specific multipart upload event

You can use the client.listParts method to list the successfully uploaded parts of a multipart upload.

import Client, { RequestError } from '@aliyun/oss';

// Create an OSS client instance.
const client = new Client({
  // Replace with the AccessKey ID from your STS temporary access credential.
  accessKeyId: 'yourAccessKeyId',
  // Replace with the AccessKey secret from your STS temporary access credential.
  accessKeySecret: 'yourAccessKeySecret',
  // Replace with the security token from your STS temporary access credential.
  securityToken: 'yourSecurityToken',
  // Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
  region: 'oss-cn-hangzhou',
});

// Specify the name of the bucket to use. Replace with your actual bucket name.
const bucket = 'yourBucketName';
// Specify the name of the object to use. Replace with your actual object name.
const key = 'yourObjectName';

/**
 * List uploaded parts.
 * Use the listParts method to list information about uploaded parts in a specific multipart upload task.
 */
const listParts = async () => {
  try {
    // Call the listParts method to list information about uploaded parts in a specific multipart upload task.
    const res = await client.listParts({
      bucket, // Bucket name
      key,    // Object (file) name
      // The uploadId can be obtained from the response of client.initiateMultipartUpload or client.listMultipartUploads.
      uploadId: 'uploadId', // Replace with the actual uploadId.
    });

    // Print information about the uploaded parts.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Catch exceptions that occur during the request.
    if (err instanceof RequestError) {
      // If a known type of error occurs, print the error code, error message, request ID, status code, EC code, and other information.
      console.log('code: ', err.code); // Error code.
      console.log('message: ', err.message); // Error message.
      console.log('requestId: ', err.requestId); // Request ID.
      console.log('status: ', err.status); // HTTP status code.
      console.log('ec: ', err.ec); // Error code.
    } else {
      // Print other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
};

// Call the listParts function to perform the list uploaded parts operation.
listParts();

List in-progress multipart upload events

You can use the client.listMultipartUploads method to list multipart uploads that are initiated but not yet completed or aborted.

import Client, { RequestError } from '@aliyun/oss';

// Create an OSS client instance.
const client = new Client({
  // Replace with the AccessKey ID from your STS temporary access credential.
  accessKeyId: 'yourAccessKeyId',
  // Replace with the AccessKey secret from your STS temporary access credential.
  accessKeySecret: 'yourAccessKeySecret',
  // Replace with the security token from your STS temporary access credential.
  securityToken: 'yourSecurityToken',
  // Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
  region: 'oss-cn-hangzhou',
});

// Specify the name of the bucket to use. Replace with your actual bucket name.
const bucket = 'yourBucketName';

/**
 * List in-progress and incomplete multipart upload tasks.
 * Use the listMultipartUploads method to list incomplete multipart upload tasks in a specific bucket.
 */
const listMultipartUploads = async () => {
  try {
    // Call the listMultipartUploads method to list incomplete multipart upload tasks in a specific bucket.
    const res = await client.listMultipartUploads({
      bucket, // Bucket name
    });

    // Print information about the incomplete multipart upload tasks.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Catch exceptions that occur during the request.
    if (err instanceof RequestError) {
      // If a known type of error occurs, print the error code, error message, request ID, status code, EC code, and other information.
      console.log('code: ', err.code); // Error code.
      console.log('message: ', err.message); // Error message.
      console.log('requestId: ', err.requestId); // Request ID.
      console.log('status: ', err.status); // HTTP status code.
      console.log('ec: ', err.ec); // Error code.
    } else {
      // Print other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
};

// Call the listMultipartUploads function to perform the list incomplete multipart upload tasks operation.
listMultipartUploads();