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

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

Parameter Description
BucketName The name of the bucket.
Key The name of the object to upload to 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.

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

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 = "https://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 type of content you want to upload.
meta.setContentType("text/plain");

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

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

// Configure a single parameter by using 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 threads for simultaneous upload. Default value: 1.
uploadFileRequest.setTaskNum(5);
// Specify the size of each part to upload.
uploadFileRequest.setPartSize(1 * 1024 * 1024);
// Enable resumable upload. By default, resumable upload is disabled.
uploadFileRequest.setEnableCheckpoint(true);
// Specify the checkpoint file that records the upload results of each part.
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);

// Shut down the OSSClient instance.
ossClient.shutdown();