Help Center/ OBS Nodejs SDK/ API Reference/ Other APIs/ Performing a Resumable Upload
Updated on 2023-11-09 GMT+08:00

Performing a Resumable Upload

API Description

This API is an encapsulated and enhanced version of multipart upload, and aims to eliminate large file upload failures caused by poor network conditions and program breakdowns.

Method Definition

ObsClient.uploadFile

Request Parameter

Field

Type

Optional or Mandatory

Description

Bucket

String

Mandatory

Bucket name

Key

String

Mandatory

Object name

UploadFile

String

Mandatory

Local file to be uploaded

PartSize

Number

Optional

Part size, in bytes. The value ranges from 100 KB to 5 GB and is 5 MB by default.

TaskNum

Number

Optional

Maximum number of threads that can be concurrently executed for upload. The default value is 20.

ProgressCallback

Function

Optional

Callback function for obtaining the upload progress

NOTE:

This callback function contains the following parameters in sequence: number of uploaded bytes, total bytes, and used time (unit: second).

ResumeCallback

Function

Optional

Callback function used to obtain the control parameter for canceling a resumable upload

NOTE:
  • This callback function contains a control parameter used for canceling resumable uploads.
  • By calling the cancel method of this control parameter, you can pause a resumable upload.

EnableCheckpoint

Boolean

Optional

Whether to enable the resumable upload mode. The default value is false, which indicates that this mode is disabled.

CheckpointFile

String

Optional

File used to record the upload progress. This parameter is effective only in the resumable upload mode. If this parameter is null, the file will be in the same directory as the local file to be uploaded.

EnableCheckSum

Boolean

Optional

Whether to verify the content of the to-be-uploaded file. This parameter is effective only in the resumable mode. The default value is false, which indicates that the content will not be verified.

ContentType

String

Optional

MIME type of the object

ACL

String

Optional

Pre-defined access control policy

WebsiteRedirectLocation

String

Optional

Location where the object is redirected to, when the bucket is configured with website hosting

SseKms

String

Optional

Algorithm used in SSE-KMS encryption. The value can be:

  • kms

SseKmsKey

String

Optional

Master key used in SSE-KMS encryption. This field can be null.

sseC

String

Optional

Algorithm used in SSE-C encryption. The value can be:

  • AES256

SseCKey

String

Optional

Key used to encrypt the object in SSE-C mode, which is calculated by using AES256

Metadata

Object

Optional

Customized metadata of the object

Returned Result (InterfaceResult)

Field

Type

Description

RequestId

String

Request ID returned by the OBS server

ETag

String

ETag calculated based on the ETags of all combined parts

Bucket

String

Bucket in which parts are combined

Key

String

Object name obtained after part combination

Location

String

URL of the object obtained after part combination

VersionId

String

Version ID of the object obtained after part combination

SseKms

String

Algorithm used in SSE-KMS encryption

SseKmsKey

String

Master key used in SSE-KMS encryption

SseC

String

Algorithm used in SSE-C encryption

SseCKeyMd5

String

MD5 value of the key used in SSE-C encryption

Sample Code

This example uploads the example file to the examplebucket bucket in a resumable upload and prints the progress.

// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');

// Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
// Obtain an AK/SK pair on the management console by referring to https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html.
const AK = process.env.AccessKeyID
const SK = process.env.SecretAccessKey
// (Optional) If you use a temporary AK/SK pair and a security token to access OBS, obtain them using environment variables.
const security_token= process.env.SecurityToken
// Set server to the endpoint corresponding to the bucket. EU-Dublin is used here as an example. Replace it with the one in your actual situation.
const server = "https://obs.eu-west-101.myhuaweicloud.com"

var obsClient = new ObsClient({
    access_key_id: AK,
    secret_access_key: SK,
    server: server,
});

const Bucket = 'examplebucket'
// Name of the object after being uploaded
const Key = 'objectname'
// Local file to be uploaded
const UploadFile = 'D:\\example'
// Part size, in bytes. The value ranges from 100 KB to 5 GB and is 5 MB by default.
const PartSize = 10 * 1024 * 1024
// Maximum number of concurrent part uploads. The default value is 20.
const TaskNum = 10
// Define the control parameter for canceling the resumable upload.
var hook;
// Perform the resumable upload.
try {
    obsClient.uploadFile({
        Bucket,
        Key,
        UploadFile,
        PartSize,
        TaskNum,
        // Enable the resumable upload.
        EnableCheckpoint: true,
        // Progress callback
        ProgressCallback: function (transferredAmount, totalAmount, totalSeconds) {
            // Print the upload speed in KB/s.
            console.log(transferredAmount * 1.0 / totalSeconds / 1024);
            // Print the upload percentage.
            console.log(transferredAmount * 100.0 / totalAmount);
            // Pause the upload when the progress reaches 50%.
            // if(hook && (transferredAmount / totalAmount) > 0.5){
            //     // Pause the resumable upload.
            //     hook.cancel();
            // }
        },
        ResumeCallback: function (resumeHook) {
            // Obtain the control parameter for canceling the resumable upload.
            hook = resumeHook;
        }
    }, (err, result) => {
        if (err) {
            console.log('UploadFile Failed')
            console.error('Error-->' + err);
        } else {
            // If status code 2xx is returned, the API is called successfully. Otherwise, the API call fails.
            if (result.CommonMsg.Status < 300) {
                console.log('UploadFile Succeeded')
                console.log('RequestId-->' + result.InterfaceResult.RequestId);
                console.log('Bucket-->' + result.InterfaceResult.Bucket);
                console.log('Key-->' + result.InterfaceResult.Key);
                console.log('Location-->' + result.InterfaceResult.Location);
            } else {
                console.log('UploadFile Failed')
                console.log('ErrorCode-->' + result.CommonMsg.Code);
                console.log('ErrorMessage-->' + result.CommonMsg.Message);
            }
        }
    });
} catch (error) {
    console.log('UploadFile Failed')
    console.error('Error-->' + error);
}