• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Android SDK)
  4. Object Download
  5. Performing a Resumable Download

Performing a Resumable Download

Downloading large files often fails due to poor network conditions or program breakdowns. It is a waste of resources to restart the download process upon a download failure, and the restarted download process may still suffer from the unstable network. To resolve such issues, you can use the API for resumable download, whose working principle is to divide the to-be-downloaded file into multiple parts and download them separately. The download result of each part is recorded in a checkpoint file in real time. Only when all parts are successfully downloaded, the result indicating a successful download will be returned. Otherwise, an exception is thrown to remind you of calling the API again for re-downloading. Based on the download status of each part recorded in the checkpoint file, the re-downloading will download the parts failed to be downloaded previously, instead of downloading all parts. By virtue of this, resources are saved and efficiency is improved.

You can call ObsClient.downloadFile to perform a resumable download. The following table describes the parameters involved in this API.

Parameter

Description

Method in OBS Android SDK

bucketName

(Mandatory) Bucket name

DownloadFileRequest.setBucketName

objectKey

(Mandatory) Object name

DownloadFileRequest.setObjectKey

downloadFile

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.

DownloadFileRequest.setDownloadFile

partSize

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

DownloadFileRequest.setPartSize

taskNum

Maximum number of threads that can be concurrently executed for downloading. The default value is 1.

DownloadFileRequest.setTaskNum

enableCheckpoint

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

DownloadFileRequest.setEnableCheckpoint

checkpointFile

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 local directory as the downloaded object.

DownloadFileRequest.setCheckpointFile

versionId

Object version

DownloadFileRequest.setVersionId

ifModifiedSince

Returns the object if it is modified after the time specified by this parameter; otherwise, an exception is thrown.

DownloadFileRequest.setIfModifiedSince

ifUnmodifiedSince

Returns the object if it remains unchanged since the time specified by this parameter; otherwise, an exception is thrown.

DownloadFileRequest.setIfUnmodifiedSince

ifMatchTag

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

DownloadFileRequest.setIfMatchTag

ifNoneMatchTag

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

DownloadFileRequest.setIfNoneMatchTag

Sample code:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
DownloadFileRequest request = new DownloadFileRequest("bucketname", "objectkey");
// Set the local path to which the object is downloaded.
request.setDownloadFile("localfile");
// Set the maximum number of threads that can be concurrently executed for downloading.
request.setTaskNum(5);
// Set the part size to 10 MB.
request.setPartSize(10 * 1024 * 1024);
// Enable resumable download.
request.setEnableCheckpoint(true);
try{
    // Perform a resumable download.
    DownloadFileResult result = obsClient.downloadFile(request);
}catch (ObsException e) {
// When an exception occurs, you can call the API for resumable download again to perform re-downloading.
}finally{
    // Close obsClient.
    obsClient.close();
}
NOTE:
  • The API for resumable download, which is implemented based on partial download, is an encapsulated and enhanced version of partial download.
  • This API saves resources and improves efficiency upon the re-download, and speeds up the download process by concurrently downloading parts. Because this API is transparent to users, users are free from concerns about internal service details, such as the creation and deletion of checkpoint files, division of objects, and concurrent download of parts.
  • The default value of the enableCheckpoint parameter is false, which indicates that the resumable download mode is disabled. In such cases, this API degrades to the simple encapsulation of partial download, and no checkpoint file will be generated.
  • checkpointFile is effective only when enableCheckpoint is true.