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 has been modified since the specified time; otherwise, an error is returned. |
request.ifModifiedSince |
ifUnmodifiedSince |
Returns the object if it has not been modified since the specified time; otherwise, an error is returned. |
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"; // 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 AccessKeyID and SecretAccessKey. // 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. char* ak_env = getenv("AccessKeyID"); char* sk_env = getenv("SecretAccessKey"); NSString *AK = [NSString stringWithUTF8String:ak_env]; NSString *SK = [NSString stringWithUTF8String:sk_env]; // 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. }
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