In a wireless network environment, a long period of time is required 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 re-uploaded. To resolve this issue, Object Storage Service (OSS) SDK for Android provides the resumable upload feature.
Overview
OSS SDK for Android provides the following methods for performing resumable upload: resumableUpload and sequenceUpload.
(Recommended) resumableUpload specifies that multiple parts are uploaded in parallel. Up to five parts can be uploaded in parallel.
sequenceUpload specifies that parts are sequentially uploaded. The next part is uploaded after the previous part is uploaded.
In this topic, the following sample code provides only examples on how to perform resumable upload by using the resumableUpload method. If you want to upload multiple objects by performing resumable upload, you must create multiple resumableUpload requests.
Usage notes
Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.
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 feature. 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 performing resumable upload, take note of the following items:
Before resumable upload
Before you upload an object to OSS by performing resumable upload, you can specify a directory for the checkpoint file that stores the resumable upload progress. The checkpoint file applies only to the current resumable upload task.
If you do not specify the directory for the checkpoint file and a part of a large object fails to be uploaded due to network issues, a long period of time is required and a large amount of traffic is consumed to re-upload the entire object.
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 the upload callback feature, which is used in the same manner as in common upload tasks. For more information, see Callback.
You can perform resumable upload by calling the following API operations: InitMultipartUpload, UploadPart, ListParts, CompleteMultipartUpload, and AbortMultipartUpload. If you want to perform resumable upload by using Security Token Service (STS), 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 completed, the uploaded parts become unnecessary in OSS. To resolve this issue, you can configure lifecycle rules for the bucket in which the parts are stored to delete the parts on a regular basis. For more information, see Configure lifecycle rules.
Examples
You can synchronously or asynchronously upload a local file to OSS by performing resumable upload.
References
For more information about how to configure lifecycle rules for a bucket, see Configure lifecycle rules.
For more information about how to implement an upload callback in resumable upload, see Callback.
For more information about how to initialize an OSSClient instance, see Initialization.