Object Storage Service (OSS) provides the multipart upload feature. Multipart upload allows you to split a large object into multiple parts to upload. After these parts are uploaded, you can call the CompleteMultipartUpload operation to combine the parts into a complete object.
Background information
When you need to upload a large file, you can use the MultipartUpload method to perform a multipart upload. This method splits an object into multiple parts that are uploaded separately. If some parts fail to upload, OSS records the upload progress. When you re-upload, you only need to upload the failed parts instead of the entire file.
To upload an object whose size is larger than 100 MB, we recommend that you use multipart upload to increase the success rate of the upload. If multipart upload is used to upload an object whose size is smaller than 100 MB and the value of the partSize parameter is inappropriate, the upload progress may not be fully displayed. To upload an object whose size is smaller than 100 MB, we recommend that you use simple upload.
When you use the MultipartUpload method, you must handle ConnectionTimeoutError errors if they occur. For example, you can handle the timeout by reducing the shard size, increasing the timeout period, retrying the request, or catching the ConnectionTimeoutError. For more information, see Network error handling.
The following table describes the parameters related to multipart uploads.
Type | Parameter | Description |
Required parameters | name {String} | The full path of the object. The full path cannot include the bucket name. |
file {String|File} | The file path or an HTML5 file. | |
[options] {Object} Optional parameters | [checkpoint] {Object} | A file that records the local multipart upload result. Set this parameter to enable resumable uploads. Progress information is saved to this file. If a part fails to upload, the next upload attempt resumes from the recorded breakpoint. The file is deleted after the upload is complete. |
[parallel] {Number} | The number of parts to upload concurrently. The default value is 5. Do not set this parameter unless necessary. | |
[partSize] {Number} | The size of each part. The value must be in the range of 100 KB to 5 GB. The default part size is 1 × 1024 × 1024 (1 MB). Do not set this parameter unless necessary. | |
[progress] {Function} | The progress callback function. It is used to obtain the upload progress. The function can be an async function. The callback function includes three parameters:
| |
[meta] {Object} | User-defined header metadata. The header prefix is | |
[mime] {String} | Sets the `Content-Type` request header. | |
[headers] {Object} | Other headers. For more information, see RFC 2616. The headers include the following:
|
Complete multipart upload example
MD5 validation is not supported for multipart uploads in Node.js. After the multipart upload is complete, you can call the CRC-64 library to perform a 64-bit cyclic redundancy check if needed.
The following code shows how to use the multipartUpload method to perform a multipart upload.
const OSS = require('ali-oss');
const path = require("path");
const client = new OSS({
// Replace yourregion with 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: 'yourregion',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourbucketname',
});
const progress = (p, _checkpoint) => {
// The upload progress of the object.
console.log(p);
// The breakpoint information for the multipart upload.
console.log(_checkpoint);
};
const headers = {
// Specify the storage class of the object.
'x-oss-storage-class': 'Standard',
// Specify tags for the object. You can specify multiple tags.
'x-oss-tagging': 'Tag1=1&Tag2=2',
// Specify whether to overwrite an object that has the same name when you initialize the multipart upload. In this example, this parameter is set to true to prevent overwriting an object that has the same name.
'x-oss-forbid-overwrite': 'true'
}
// Start the multipart upload.
async function multipartUpload() {
try {
// Specify the full path of the object (for example, exampledir/exampleobject.txt) and the full path of the local file (for example, D:\\localpath\\examplefile.txt). The full path of the object cannot contain the bucket name.
// If you do not specify a local path in the full path of the local file (for example, examplefile.txt), the file is uploaded from the local path that corresponds to the project where the sample program is located.
const result = await client.multipartUpload('exampledir/exampleobject.txt', path.normalize('D:\\localpath\\examplefile.txt'), {
progress,
// headers,
// Specify the meta parameter to customize the metadata of the object. You can use the head operation to obtain the metadata of the object.
meta: {
year: 2020,
people: 'test',
},
});
console.log(result);
// Specify the full path of the object, for example, exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name.
const head = await client.head('exampledir/exampleobject.txt');
console.log(head);
} catch (e) {
// Catch the timeout exception.
if (e.code === 'ConnectionTimeoutError') {
console.log('TimeoutError');
// do ConnectionTimeoutError operation
}
console.log(e);
}
}
multipartUpload();The multipartUpload method used in the preceding example encapsulates three API operations: initializing a multipart upload, uploading parts, and completing a multipart upload. If you want to implement a multipart upload in steps, you can call the .initMultipartUpload, .uploadPart, and .completeMultipartUpload methods in sequence.
Cancel a multipart upload event
You can call the client.abortMultipartUpload method to cancel a multipart upload task. After a multipart upload task is canceled, you can no longer use the upload ID to upload parts, and the uploaded parts are deleted.
The following code shows how to cancel a multipart upload.
const OSS = require("ali-oss");
const client = new OSS({
// Replace yourregion with 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: "yourregion",
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: "yourbucketname",
});
async function abortMultipartUpload() {
// Specify the full path of the object, for example, exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name.
const name = "exampledir/exampleobject.txt";
// Specify the upload ID. The upload ID is obtained from the response to the call of InitiateMultipartUpload to initialize the multipart upload.
const uploadId = "0004B999EF518A1FE585B0C9360D****";
const result = await client.abortMultipartUpload(name, uploadId);
console.log(result);
}
abortMultipartUpload();
List multipart upload events
You can call the client.listUploads method to list all in-progress multipart uploads. In-progress multipart uploads are uploads that have been initialized but are not yet completed or canceled.
const OSS = require("ali-oss");
const client = new OSS({
// Replace yourregion with 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: "yourregion",
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: "yourbucketname",
});
async function listUploads(query = {}) {
// You can set parameters such as prefix, marker, delimiter, upload-id-marker, and max-uploads in the query.
const result = await client.listUploads(query);
result.uploads.forEach((upload) => {
// The upload ID of the multipart upload.
console.log(upload.uploadId);
// Combine all uploaded parts into a complete object and specify the full path of the object.
console.log(upload.name);
});
}
const query = {
// Specify the maximum number of multipart upload events to return. The default and maximum value of the max-uploads parameter is 1000.
"max-uploads": 1000,
};
listUploads(query);
List uploaded parts
During a multipart upload, you can call the client.listParts method to list all successfully uploaded parts for a specified upload ID.
const OSS = require("ali-oss");
const client = new OSS({
// Replace yourregion with 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: "yourregion",
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: "yourbucketname",
});
async function listParts() {
const query = {
// Specify the maximum number of parts to return. The default and maximum value of the max-parts parameter is 1000.
"max-parts": 1000,
};
let result;
do {
result = await client.listParts(
// Specify the full path of the object (for example, exampledir/exampleobject.txt). The full path of the object cannot contain the bucket name.
"exampledir/exampleobject.txt",
// The upload ID is obtained from the response after you call InitiateMultipartUpload to initialize the multipart upload and before you call CompleteMultipartUpload to complete the multipart upload.
"0004B999EF518A1FE585B0C9360D****",
query
);
// Specify the starting position for the next list operation. Only parts with part numbers greater than this value are listed.
query["part-number-marker"] = result.nextPartNumberMarker;
result.parts.forEach((part) => {
console.log(part.PartNumber);
console.log(part.LastModified);
console.log(part.ETag);
console.log(part.Size);
});
} while (result.isTruncated === "true");
}
listParts();References
For the complete sample code for multipart upload, see GitHub examples.
The
multipartUploadmethod in the Node.js SDK encapsulates the following three API operations:For more information about the API operation to initialize a multipart upload, see InitiateMultipartUpload.
For more information about the API operation to upload a part, see UploadPart.
For more information about the API operation to complete a multipart upload, see CompleteMultipartUpload.
For more information about the API operation to cancel a multipart upload, see AbortMultipartUpload.
For more information about the API operation to list uploaded parts, see ListParts.
For more information about the API operation to list all in-progress multipart uploads (uploads that are initialized but not yet completed or canceled), see ListMultipartUploads.