Before you upload an object to OSS by using resumable upload, you can specify a folder for the checkpoint file that stores resumable upload records. If a file fails to be uploaded because the network is abnormal or the program stops responding, the upload task is resumed from the position recorded in the checkpoint file to upload remaining bytes.

Background information

For more information about resumable upload, see Multipart upload and resumable upload. For the complete code, visit GitHub.

You can use ossClient.uploadFile to implement resumable upload. The following table describes the parameters you can configure for uploadFileRequest.

Parameter Descripton
BucketName The name of the bucket.
Key The name of the object in OSS.
UploadFile The path of the local file to upload to OSS.
TaskNum The number of concurrent threads for the resumable upload task. Default value: 1.
PartSize The size of each part. Valid values: 100 KB to 5 GB. Default value: 100 KB.
EnableCheckpoint Specifies whether to enable resumable upload. The default value is false.
CheckpointFile The checkpoint file that records the upload result of each part. This file stores information about upload progress. If a part fails to be uploaded, the task can be continued based on the progress recorded in the checkpoint file. After the entire local file is uploaded, the checkpoint file is deleted. By default, if you do not specify this parameter, this checkpoint file shares the same directory named ${uploadFile}.ucp as the file you want to upload.

Sample code

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

// The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. 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);

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

// Configure parameters with UploadFileRequest.
UploadFileRequest uploadFileRequest = new UploadFileRequest("<yourBucketName>","<yourObjectName>");

// Configure a single parameter with UploadFileRequest.
// Specify the bucket name.
//uploadFileRequest.setBucketName("<yourBucketName>");
// Specify the object name.
//uploadFileRequest.setKey("<yourObjectName>");
// Specify the local file to upload.
uploadFileRequest.setUploadFile("<yourLocalFile>");
// Specify the number of concurrent threads for the resumable upload task. The default value is 1.
uploadFileRequest.setTaskNum(5);
// Specify the size of each part to upload.
uploadFileRequest.setPartSize(1 * 1024 * 1024);
// Start resumable upload, which is disabled by default.
uploadFileRequest.setEnableCheckpoint(true);
// Configure the file used to record the result of local part uploads.
uploadFileRequest.setCheckpointFile("<yourCheckpointFile>");
// Configure object metadata.
uploadFileRequest.setObjectMetadata(meta);
// Configure callback. Set the yourCallbackEvent parameter to Callback.
uploadFileRequest.setCallback("<yourCallbackEvent>");

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

// Close your OSSClient.
ossClient.shutdown();