In a wireless network environment, it takes a long period of time to upload a large object and the upload may fail due to poor network connectivity or network switchover. If the upload fails, the entire object must be uploaded again. To address this problem, OSS SDK for iOS provides the resumable upload feature.

Background information

We recommend that you do not use resumable upload when you upload objects that are smaller than 5 GB in size from a mobile device. Resumable upload is implemented by using the multipart upload method. Resumable upload of a single object requires multiple network requests, which is inefficient. When you upload an object that is larger than 5 GB in size by using resumable upload, take note of the following items:

  • Before resumable upload

    Before you upload an object to OSS by using the resumable upload method, you can specify a directory for the checkpoint file that stores resumable upload records. The checkpoint file applies only to the current resumable upload task.

    • If you do not specify the directory for the checkpoint file and part of a large object fails to be uploaded due to network issues, it takes a large amount of time and consumes a large amount of traffic to upload the entire large object again.
    • If you specify the directory for the checkpoint file, a failed resumable upload task can be resumed from the position recorded in the checkpoint file.
  • During resumable upload
    • Resumable upload allows you to upload only local files. Resumable upload supports upload callback. You can use resumable upload in a similar way you use upload callback. For more information, see Callback.
    • Resumable upload is implemented by calling the following API operations: InitMultipartUpload, UploadPart, ListParts, CompleteMultipartUpload, and AbortMultipartUpload. If you want to use the Security Token Service (STS) authentication mode to perform resumable upload tasks, make sure that you are authorized to call the preceding API operations.
    • By default, the MD5 hash of each part is verified in a resumable upload task. Therefore, you do not need to specify the Content-Md5 header in the request.
    • If a resumable upload task fails and is not resumed for an extended period of time, the uploaded parts may become useless in OSS. You can configure lifecycle rules for a bucket to delete expired parts. For more information, see Manage lifecycle rules.
      Important By default, if the current upload task is canceled, the parts uploaded to the server are deleted synchronously. If you want to retain resumable upload records, you must specify a folder to store the checkpoint file that contains the resumable upload records and modify the setting of the deleteUploadIdOnCancelling parameter. If resumable upload records are retained on the mobile device for an extended period of time but the parts uploaded to the server have been deleted by the configured lifecycle rules of the bucket, the resumable upload records on the server may be different from those on the mobile device.

Examples

  • The following code provides an example on how to call the resumableUpload method to perform a resumable upload task when the checkpoint file is permanently stored on the local device:
    OSSResumableUploadRequest * resumableUpload = [OSSResumableUploadRequest new];
    resumableUpload.bucketName = OSS_BUCKET_PRIVATE;
    //...
    NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    resumableUpload.recordDirectoryPath = cachesDir;
                        
    Note When a resumable upload task fails, a checkpoint file is generated to record the resumable upload progress. The next upload starts from the position recorded in the checkpoint file to upload the entire object. After the object is uploaded, the checkpoint file is automatically deleted. By default, the checkpoint file is not retained permanently on the local device.
  • The following code provides an example on how to perform a resumable upload task:
    // Obtain an upload ID to upload an object. 
    OSSResumableUploadRequest * resumableUpload = [OSSResumableUploadRequest new];
    resumableUpload.bucketName = <bucketName>;
    // objectKey is equivalent to objectName that indicates the full path of the object you want to upload to OSS by using resumable upload. The path must include the extension of the object name. For example, you can set objectKey to abc/efg/123.jpg.
    resumableUpload.objectKey = <objectKey>;
    resumableUpload.partSize = 1024 * 1024;
    resumableUpload.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
        NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
    };
    NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    // Specify the path to store the checkpoint file. 
    resumableUpload.recordDirectoryPath = cachesDir;
    // Set the deleteUploadIdOnCancelling parameter to NO. NO indicates that the checkpoint file is not deleted when the upload task fails. The next upload starts from the position recorded in the checkpoint file to upload the entire object. If you do not specify this parameter, the default value YES is used. YES indicates that the checkpoint file is deleted when the upload task fails. The entire object is re-uploaded during the next upload. 
    resumableUpload.deleteUploadIdOnCancelling = NO;
    
    resumableUpload.uploadingFileURL = [NSURL fileURLWithPath:<your file path>];
    OSSTask * resumeTask = [client resumableUpload:resumableUpload];
    [resumeTask continueWithBlock:^id(OSSTask *task) {
        if (task.error) {
            NSLog(@"error: %@", task.error);
            if ([task.error.domain isEqualToString:OSSClientErrorDomain] && task.error.code == OSSClientErrorCodeCannotResumeUpload) {
                // The task cannot be resumed. You must obtain a new upload ID to upload the object. 
            }
        } else {
            NSLog(@"Upload file success");
        }
        return nil;
    }];
    
    // [resumeTask waitUntilFinished];
    
    // [resumableUpload cancel];