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

Background information

We recommend that you do not use resumable upload when you upload objects smaller than 5 GB in size from a mobile device. Resumable upload is implemented based on the multipart upload method. Resumable upload of a single object requires multiple network requests, which is inefficient. The following section provides the precautions you need to understand before and during resumable upload.

  • Before resumable upload

    Before you upload an object to OSS by using the resumable upload method, you can specify a folder 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 folder for the checkpoint file and part of a large object fails to be uploaded due to network problems, 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 folder for the checkpoint file, a failed resumable upload task can be resumed from the position recorded in the checkpoint file.
  • During resumable upload

    When you use resumable upload, you must understand the following information:

    • Resumable upload allows you to upload only local files. Resumable upload supports the upload callback feature, which is used in the same way as it is in common upload tasks. 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 STS authentication mode to perform resumable upload tasks, ensure 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.
    • When a resumable upload task fails, the uploaded parts become useless in OSS. You can configure lifecycle rules for a bucket to delete expired parts. For more information, see Manage lifecycle rules.

Examples

The following sections provide examples on how to use resumable upload.

  • The following code provides an example on how to call the ResumableUploadRequest method to perform a resumable upload task when the checkpoint file is not permanently stored in the local device:
    // Create a resumable upload request.
    // objectKey is equivalent to objectName that indicates the complete path of the object that you want to upload to OSS by using resumable upload. The path must include the extension of the object. For example, you can set objectKey to abc/efg/123.jpg.
    ResumableUploadRequest request = new ResumableUploadRequest("<bucketName>", "<objectKey>", "<uploadFilePath>");
    // Configure upload callback.
    request.setProgressCallback(new OSSProgressCallback<ResumableUploadRequest>() {
        @Override
        public void onProgress(ResumableUploadRequest request, long currentSize, long totalSize) {
            Log.d("resumableUpload", "currentSize: " + currentSize + " totalSize: " + totalSize);
        }
    });
    // Call the asyncResumableUpload method to perform the resumable upload task.
    OSSAsyncTask resumableTask = oss.asyncResumableUpload(request, new OSSCompletedCallback<ResumableUploadRequest, ResumableUploadResult>() {
        @Override
        public void onSuccess(ResumableUploadRequest request, ResumableUploadResult result) {
            Log.d("resumableUpload", "success!") ;
        }
    
        @Override
        public void onFailure(ResumableUploadRequest request, ClientException clientExcepion, ServiceException serviceException) {
            // Handle exceptions.
        }
    });
    
    // Wait until the resumable upload task is complete.
    resumableTask.waitUntilFinished(); 
    					
  • The following code provides an example on how to call the ResumableUploadRequest method to perform a resumable upload task when the checkpoint file is permanently stored in the local device:
    String recordDirectory = Environment.getExternalStorageDirectory().getAbsolutePath() + "/oss_record/";
    
    File recordDir = new File(recordDirectory);
    
    // Ensure that the folder for the checkpoint file exists. If the folder does not exist, create a folder.
    if (! recordDir.exists()) {
        recordDir.mkdirs();
    }
    
    // Create a resumable upload request and specify the absolute path of the checkpoint file.
    ResumableUploadRequest request = new ResumableUploadRequest("<bucketName>", "<objectKey>", "<uploadFilePath>", recordDirectory);
    // Configure upload callback.
    request.setProgressCallback(new OSSProgressCallback<ResumableUploadRequest>() {
        @Override
        public void onProgress(ResumableUploadRequest request, long currentSize, long totalSize) {
            Log.d("resumableUpload", "currentSize: " + currentSize + " totalSize: " + totalSize);
        }
    });
    
    
    OSSAsyncTask resumableTask = oss.asyncResumableUpload(request, new OSSCompletedCallback<ResumableUploadRequest, ResumableUploadResult>() {
        @Override
        public void onSuccess(ResumableUploadRequest request, ResumableUploadResult result) {
            Log.d("resumableUpload", "success!") ;
        }
    
        @Override
        public void onFailure(ResumableUploadRequest request, ClientException clientExcepion, ServiceException serviceException) {
            // Handle exceptions.
        }
    });
    
    // Wait until the resumable upload task is complete.
    resumableTask.waitUntilFinished();                
  • The following code provides a complete example on how to perform a resumable upload task:
    String recordDirectory = Environment.getExternalStorageDirectory().getAbsolutePath() + "/oss_record/";
    
    File recordDir = new File(recordDirectory);
    
    // Ensure that the folder for the checkpoint file exists. If the folder does not exist, create a folder.
    if (! recordDir.exists()) {
        recordDir.mkdirs();
    }
    
    // Create a resumable upload request and specify the absolute path of the checkpoint file.
    ResumableUploadRequest request = new ResumableUploadRequest("<bucketName>", "<objectKey>", "<uploadFilePath>", recordDirectory);
    // Set the DeleteUploadOnCancelling parameter when the OSSAsyncTask cancel() method is called. When this parameter is set to false, the checkpoint file is retained. If you do not specify this parameter, default value true is used, which indicates that the checkpoint file is deleted. The entire object will be uploaded again during the next upload.
    request.setDeleteUploadOnCancelling(false);
    // Configure upload callback.
    request.setProgressCallback(new OSSProgressCallback<ResumableUploadRequest>() {
        @Override
        public void onProgress(ResumableUploadRequest request, long currentSize, long totalSize) {
            Log.d("resumableUpload", "currentSize: " + currentSize + " totalSize: " + totalSize);
        }
    });
    
    
    OSSAsyncTask resumableTask = oss.asyncResumableUpload(request, new OSSCompletedCallback<ResumableUploadRequest, ResumableUploadResult>() {
        @Override
        public void onSuccess(ResumableUploadRequest request, ResumableUploadResult result) {
            Log.d("resumableUpload", "success!") ;
        }
    
        @Override
        public void onFailure(ResumableUploadRequest request, ClientException clientExcepion, ServiceException serviceException) {
            // Handle exceptions.
        }
    });
    
    // Wait until the resumable upload task is complete.
    resumableTask.waitUntilFinished();