Performing a Resumable Upload
If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference.
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 will be 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 ObsClient.uploadFile to perform a resumable upload. The following table describes the parameters involved in this API.
Parameter |
Description |
Method in OBS Android SDK |
---|---|---|
bucketName |
(Mandatory) Bucket name |
UploadFileRequest.setBucketName |
objectKey |
(Mandatory) Object name |
UploadFileRequest.setObjectKey |
uploadFile |
(Mandatory) Local file to be uploaded |
UploadFileRequest.setUploadFile |
partSize |
Part size, in bytes. The value ranges from 100 KB to 5 GB and defaults to 5 MB. |
UploadFileRequest.setPartSize |
taskNum |
Maximum number of threads that can be concurrently executed for uploading. The default value is 1. |
UploadFileRequest.setTaskNum |
enableCheckpoint |
Whether to enable the resumable upload mode. The default value is false, which indicates that this mode is disabled. |
UploadFileRequest.setEnableCheckpoint |
isEncodeHeaders |
Whether to automatically encode the request header. |
UploadFileRequest.setIsEncodeHeaders |
checkpointFile |
File used to record the upload progress. This parameter is effective only in the resumable upload mode. If this parameter is null, the file will be in the same directory as the local file to be uploaded. |
UploadFileRequest.setCheckpointFile |
objectMetadata |
Object properties |
UploadFileRequest.setObjectMetadata |
enableCheckSum |
Whether to verify the content of the to-be-uploaded file. This parameter is effective only in the resumable upload mode. The default value is false, which indicates that the content will not be verified. |
UploadFileRequest.setEnableCheckSum |
progressListener |
Configure the data transmission listener to obtain upload progresses. |
UploadFileRequest.setProgressListener |
Sample code:
// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // 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. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // Create an instance of ObsClient. ObsClient obsClient = new ObsClient(ak, sk, endPoint); UploadFileRequest request = new UploadFileRequest("bucketname", "objectname"); // Set the large file to be uploaded. localfile is the path of the local file to be uploaded. You need to specify the file name. request.setUploadFile("localfile"); // Set the maximum number of threads that can be concurrently uploaded. request.setTaskNum(5); // Set the part size to 10 MB. request.setPartSize(10 * 1024 * 1024); // Enable resumable upload. request.setEnableCheckpoint(true); try{ // Perform a resumable upload. CompleteMultipartUploadResult result = obsClient.uploadFile(request); }catch (ObsException e) { // When an exception occurs, you can call the API for resumable upload again to perform re-uploading. }
- 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 false, which indicates that the resumable upload mode is disabled. In such cases, this API degrades to the simple encapsulation of multipart upload, and no checkpoint file will be generated.
- checkpointFile and enableCheckSum are effective only when enableCheckpoint is true.
- Due to HTTP coding restrictions, non-ASCII characters cannot be sent. If your request headers contain full-width characters, the SDK will URL encode these characters before sending the request. When you use a browser to access the object metadata, the browser automatically decodes the data.
- If you do not need the SDK to decode for you, call UploadFileRequest.setIsEncodeHeaders(false) to disable auto encoding.
- A resumable upload cannot be paused or canceled.
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