Performing a Resumable Upload
Uploading large files often fails due to poor network conditions or program breakdowns. It is a waste of resources to restart the upload process upon an upload failure, and the restarted upload process may still suffer from the unstable network. To resolve such issues, you can use the API for resumable upload, whose working principle is to divide the to-be-uploaded file into multiple parts and upload them separately. The upload result of each part is recorded in a checkpoint file in real time. Only when all parts are successfully uploaded, the result indicating a successful upload is returned. Otherwise, an exception is thrown to remind you of calling the API again for re-uploading. Based on the upload status of each part recorded in the checkpoint file, the re-uploading will upload the parts failed to be uploaded previously, instead of uploading all parts. By virtue of this, resources are saved and efficiency is improved.
You can call uploadFile to perform a resumable upload. 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 |
| objectACLPolicy | Object access control policy | request.objectACLPolicy |
| storageClass | Object storage class | request.storageClass |
| metaDataDict | Object metadata | request.metaDataDict |
| websiteRedirectLocation | Redirection location | request.websiteRedirectLocation |
| encryption | Encryption mode | request.encryption |
| 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. The default value is NO, which indicates that MD5 verification is disabled. | request.enableMD5Check |
| checkpointFilePath | File used to record the upload progress. This parameter is effective only in the resumable upload mode. If the value is null, the file is in the same directory as the local file to be uploaded. The file name extension can be set to obsuploadcheckpoint. | request.checkpointFilePath |
| partSize | Part size, in bytes. The value ranges from 100 KB to 5 GB and defaults to 5 MB. | request.partSize |
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];
// Set the maximum number of files that can be uploaded concurrently in multipart mode.
client.configuration.maxConcurrentUploadRequestCount = 5;
// Maximum number of connections for multipart upload requests.
client.configuration.uploadSessionConfiguration.HTTPMaximumConnectionsPerHost = 10;
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"fileName" ofType:@"Type"];
OBSUploadFileRequest *request = [[OBSUploadFileRequest alloc]initWithBucketName:@"bucketname" objectKey:@"objectname" uploadFilePath:filePath];
// Set the part size to 5 MB.
request.partSize = [NSNumber numberWithInteger: 5 * 1024*1024];
// Enable resumable upload.
request.enableCheckpoint = YES;
// Specify the checkpoint file path.
request.checkpointFilePath = @"Your CheckPoint File"
// Upload a file.
request.uploadProgressBlock = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%0.1f%%",(float)floor(totalBytesSent*10000/totalBytesExpectedToSend)/100);
};
OBSBFTask *task = [client uploadFile:request completionHandler:^(OBSUploadFileResponse *response, NSError *error) {
NSLog(@"%@",response);
}];
[task waitUntilFinished];
if(task.error){
// Continue to upload the file.
}
- The API for resumable upload, which is implemented based on multipart upload, is an encapsulated and enhanced version of multipart upload.
- This API saves resources and improves efficiency upon the re-upload, and speeds up the upload process by concurrently uploading 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 upload of parts.
- The default value of the enableCheckpoint parameter is NO, which indicates that the resumable upload mode is disabled. In such cases, the API for resumable upload degrades to the simple encapsulation of multipart upload, and no checkpoint file will be generated.
- checkpointFile is effective only when enableCheckpoint is YES.
- Currently, when multiple resumable upload tasks need to be executed concurrently, an independent instance of OBSClient needs to be initialized for each upload task to process requests.
Last Article: Performing a Multipart Copy
Next Article: Object Download
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.