Performing a Resumable Download
If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference.
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 |
isEncodeHeaders |
Whether to automatically decode the response header. |
DownloadFileRequest.setIsEncodeHeaders |
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 has been modified since the specified time; otherwise, an error is returned. |
DownloadFileRequest.setIfModifiedSince |
ifUnmodifiedSince |
Returns the object if it has not been modified since the specified time; otherwise, an error is returned. |
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 |
progressListener |
Configure the data transmission listener to obtain download progresses. |
DownloadFileRequest.setProgressListener |
Sample code:
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // Create an instance of ObsClient. ObsClient obsClient = new ObsClient(ak, sk, endPoint); DownloadFileRequest request = new DownloadFileRequest("bucketname", "objectname"); // 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. }
- 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.
- Due to HTTP coding restrictions, non-ASCII characters cannot be sent. The SDK uses a URL to decode the information in the response header. For example, if content-disposition in your metadata is set to attachment; filename="%E4%B8%AD%E6%96%87.txt", the result obtained by the SDK is attachment; filename="Chinese characters.txt".
- If you do not need the SDK to decode for you, call DownloadFileRequest.setIsEncodeHeaders(false) to disable auto decoding.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot