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 is 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 use downloadFile to perform a resumable download. The following table describes the parameters involved in this API.
| Parameter | Description | Method in OBS iOS SDK |
|---|---|---|
| bucketName | (Mandatory) Bucket name | request.bucketName |
| objectKey | (Mandatory) Object name | request.objectKey |
| downloadFilePath | Full path of the local directory to which the object is downloaded | request.downloadFilePath |
| versionID | Object version ID | request.versionID |
| enableCheckpoint | Whether to enable the resumable upload mode. The default value is NO, which indicates that this mode is disabled. | request.enableCheckpoint |
| enableMD5Check | Whether to enable MD5 verification | request.enableMD5Check |
| enableForceOverwrite | Whether to enable forcible overwriting | request.enableForceOverwrite |
| checkpointFilePath | File used to record the download progress. This parameter is effective only in the resumable download mode. If the value is null, the file is in the same local directory as the downloaded object. | request.checkpointFilePath |
| partSize | Part size, in bytes. The value ranges from 5 MB to 5 GB. | request.partSize |
| ifModifiedSince | Returns the object if it is modified after the time specified by this parameter; otherwise, an exception is thrown. | request.ifModifiedSince |
| ifUnmodifiedSince | Returns the object if it remains unchanged since the time specified by this parameter; otherwise, an exception is thrown. | request.ifUnmodifiedSince |
| ifETagMatch | Returns the source object if its ETag is the same as the one specified by this parameter; otherwise, an exception is thrown. | request.ifETagMatch |
| ifETagNoneMatch | Returns the source object if its ETag is different from the one specified by this parameter; otherwise, an exception is thrown. | request.ifETagNoneMatch |
Sample code:
static OBSClient *client;
NSString *endPoint = @"your-endpoint";
NSString *SK = @"*** Provide your Secret Key ***";
NSString *AK = @"*** Provide your Access Key ***";
// Initialize identity authentication.
OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK];
//Initialize service configuration.
OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider];
// Initialize an instance of OBSClient.
client = [[OBSClient alloc] initWithConfiguration:conf];
// Storage path
NSString * outfilePath = [NSTemporaryDirectory() stringByAppendingString:@"filename"];
// Maximum number of resumable downloads that can be currently performed
self.client.configuration.maxConcurrentDownloadRequestCount = 5;
// Resumable download
OBSDownloadFileRequest *request = [[OBSDownloadFileRequest alloc]initWithBucketName:@"bucketname" objectKey:@"objectname" downloadFilePath:outfilePath];
// Whether to enable forcible overwriting
request.enableForceOverwrite = YES;
// Part size
request.partSize = [NSNumber numberWithInteger:5*1024*1024];
// Whether to enable resumable download
request.enableCheckpoint = YES;
request.downloadProgressBlock = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
NSLog(@"%0.1f%%",(float)floor(totalBytesWritten*10000/totalBytesExpectedToWrite)/100);
};
OBSBFTask *task = [client downloadFile:request completionHandler:^(OBSDownloadFileResponse *response, NSError *error) {
NSLog(@"%@",response);
}];
[task waitUntilFinished];
if(task.error){
// Perform the download again.
} Last Article: Downloading an Archive Object
Next Article: Object Management
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.