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

Performing a Resumable Download

API Description

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

Method Definition

ObsClient.downloadFile

Request Parameter

Field

Type

Optional or Mandatory

Description

Bucket

String

Mandatory

Bucket name

Key

String

Mandatory

Object name

DownloadFile

String

Optional

Local path to which the object is downloaded. If this parameter is null, the downloaded object is saved in the directory where the program is executed.

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 download. The default value is 20.

ProgressCallback

Function

Optional

Callback function for obtaining the download progress

NOTE:

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

ResumeCallback

Function

Optional

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

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

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 download progress. This parameter is effective only in the resumable download mode. If this parameter is null, the file will be in the same directory as the local directory of the downloaded file.

VersionId

String

Optional

Object version ID

IfModifiedSince

String

Optional

Returns the object if it is modified after the time specified by this parameter; otherwise, an error code is returned.

IfUnmodifiedSince

String

Optional

Returns the object if it remains unchanged since the time specified by this parameter; otherwise, an error code is returned.

IfMatch

String

Optional

Returns the source object if its ETag is the same as the one specified by this parameter; otherwise, an error code is returned.

IfNoneMatch

String

Optional

Returns the source object if its ETag is different from the one specified by this parameter; otherwise, an error code is returned.

SseC

String

Optional

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

  • AES256

SseCKey

String

Optional

Key used in SSE-C decryption, which is calculated by using AES256

Returned Result (InterfaceResult)

Field

Type

Description

RequestId

String

Request ID returned by the OBS server

DeleteMarker

String

Whether the deleted object is a delete marker

LastModified

String

Time when the last modification was made to the object

CacheControl

String

Cache-Control header in the response

ContentDisposition

String

Content-Disposition header in the response

ContentEncoding

String

Content-Encoding header in the response

ContentLanguage

String

Content-Language header in the response

ContentType

String

MIME type of the object

Expires

String

Expires header in the response

ETag

String

Object ETag

VersionId

String

Object version ID

WebsiteRedirectLocation

String

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

StorageClass

String

Object storage class. If the storage class is Standard, this parameter is left blank.

Restore

String

Restore status of the object in the OBS Archive storage class

SseKms

String

Algorithm used in SSE-KMS decryption

SseKmsKey

String

Master key used in SSE-KMS decryption

SseC

String

Algorithm used in SSE-C decryption

SseCKeyMd5

String

MD5 value of the key used in SSE-C decryption

Expiration

String

Expiration details

Metadata

Object

Customized metadata of the object

Sample Code

This example downloads objectname from examplebucket in a resumable download 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 to be downloaded
const Key = 'objectname'
// Set the local path the object will be downloaded to.
const DownloadFile = 'D:\\example'
// Set the part size to 10 MB.
const PartSize = 10 * 1024 * 1024
// Number of concurrent downloads
const TaskNum = 10
// Define the control parameter used for canceling the resumable download.
var hook;
// Perform the resumable download.
try {
    obsClient.downloadFile({
        Bucket,
        Key,
        DownloadFile,
        PartSize,
        TaskNum,
        // Enable the resumable download.
        EnableCheckpoint: true,
        // Progress callback
        ProgressCallback: function (transferredAmount, totalAmount, totalSeconds) {
            // Print the download speed in KB/s.
            console.log(transferredAmount * 1.0 / totalSeconds / 1024);
            // Print the download percentage.
            console.log(transferredAmount * 100.0 / totalAmount);
            // Pause the download when the progress reaches 50%.
            // if(hook && (transferredAmount / totalAmount) > 0.5){
            //     // Pause the resumable download.
            //     hook.cancel();
            // }
        },
        ResumeCallback : function(resumeHook){
            // Obtain the control parameter used for canceling the resumable download.
            hook = resumeHook;
        }
    }, (err, result) => {
        if (err) {
            console.log('DownloadFile Failed')
            console.error('Error-->' + err);
        } else {
            if (result.CommonMsg.Status < 300) {
                console.log('DownloadFile Succeeded')
                console.log('RequestId-->' + result.InterfaceResult.RequestId);
                console.log('LastModified-->' + result.InterfaceResult.LastModified);
                console.log('Metadata-->' + JSON.stringify(result.InterfaceResult.Metadata)); 
            } else {
                console.log('DownloadFile Failed')
                console.log('ErrorCode-->' + result.CommonMsg.Code);
                console.log('ErrorMessage-->' + result.CommonMsg.Message);
            }
        }
    });
} catch (error) {
    console.log('DownloadFile Failed')
    console.error('Error-->' + error);
}