Before you upload an object to Object Storage Service (OSS) by using resumable upload, you can specify a directory for the checkpoint file that stores resumable upload records. If an object fails to upload due to a network exception or program error, the upload task is resumed from the position recorded in the checkpoint file to upload the remaining data.

Examples

The following code provides an example on how to perform resumable upload by using multipart upload:

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;

public class Demo {
    public static void main(String[] args) {
        // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
        String accessKeyId = "yourAccessKeyId";
        String accessKeySecret = "yourAccessKeySecret";

        // Create an OSSClient instance. 
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {

            ObjectMetadata meta = new ObjectMetadata();
            // Specify the type of content that you want to upload. 
            meta.setContentType("text/plain");

            // Specify the access control list (ACL) of the object to upload. 
            // meta.setObjectAcl(CannedAccessControlList.Private);

            // Configure parameters by using UploadFileRequest. 
            // Specify the bucket name and the full path of the object, and the full path cannot contain the bucket name. Examples: examplebucket for the bucket name and exampledir/exampleobject.txt for the path of the object. 
            UploadFileRequest uploadFileRequest = new UploadFileRequest("examplebucket","exampledir/exampleobject.txt");

            // Configure a single parameter by using UploadFileRequest.            
            // Specify the full path of the local file. Example: D:\\localpath\\examplefile.txt. By default, if you do not specify the full path of the local file, the local file is uploaded from the path of the project to which the sample program belongs. 
            uploadFileRequest.setUploadFile("D:\\localpath\\examplefile.txt");
            // Specify the number of threads that can be concurrently processed. Default value: 1. 
            uploadFileRequest.setTaskNum(5);
            // Specify the size of each part. Unit: bytes. Valid values: 100 KB to 5 GB. Default value: 100 KB. 
            uploadFileRequest.setPartSize(1 * 1024 * 1024);
            // Specify whether to enable resumable upload. By default, resumable upload is disabled. 
            uploadFileRequest.setEnableCheckpoint(true);
            // Specify the checkpoint file that records the upload result of each part. This file stores information about upload progress. If a part fails to upload, the task can be continued based on the progress recorded in the checkpoint file. After the object is uploaded, the checkpoint file is deleted. 
            // By default, if you do not specify this parameter, this checkpoint file shares the same directory as the object you want to upload. The directory is named ${uploadFile}.ucp. 
            uploadFileRequest.setCheckpointFile("yourCheckpointFile");
            // Configure object metadata. 
            uploadFileRequest.setObjectMetadata(meta);
            // Configure upload callback. The parameter type is Callback. 
            //uploadFileRequest.setCallback("yourCallbackEvent");

            // Start resumable upload. 
            ossClient.uploadFile(uploadFileRequest);

        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (Throwable ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
                // Shut down the OSSClient instance. 
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}           

References

For more information about the complete sample code for resumable upload, visit GitHub.