All Products
Search
Document Center

Object Storage Service:Resumable upload (Java SDK V1)

Last Updated:Nov 25, 2025

When you use resumable upload to upload a file to Object Storage Service (OSS), you can specify a checkpoint file. If the upload is interrupted by a network error or program crash, the process resumes from the position recorded in the checkpoint file.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see OSS regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configuration examples for common scenarios.

  • To perform a resumable upload, you must have the oss:PutObject permission. For more information, see Attach a custom policy to a RAM user.

Sample code

The following code provides an example of how to perform a resumable upload.

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;

public class UploadFile {
        public static void main(String[] args) throws Exception {
            // This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint.
            String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
            // Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
            String region = "cn-hangzhou";
            // Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
            EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

            // Create an OSSClient instance.
            // When the OSSClient instance is no longer needed, call the shutdown method to release resources.
            ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
            clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
            OSS ossClient = OSSClientBuilder.create()
                    .endpoint(endpoint)
                    .credentialsProvider(credentialsProvider)
                    .clientConfiguration(clientBuilderConfiguration)
                    .region(region)
                    .build();

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

                // Set the access control list (ACL) for the object during upload.
                // meta.setObjectAcl(CannedAccessControlList.Private);

                // Set multiple parameters using UploadFileRequest.
                // Specify the bucket name, such as examplebucket, and the full path of the object, such as exampledir/exampleobject.txt. The full path cannot contain the bucket name.
                UploadFileRequest uploadFileRequest = new UploadFileRequest("examplebucket","exampledir/exampleobject.txt");

                // Set a single parameter using UploadFileRequest.
                // Specify the full path of the local file, for example, D:\\localpath\\examplefile.txt. If you do not specify the local path, the file is uploaded from the path of the project where the sample program is located.
                uploadFileRequest.setUploadFile("D:\\localpath\\examplefile.txt");
                // Specify the number of concurrent threads for the upload. The default value is 1.
                uploadFileRequest.setTaskNum(5);
                // Specify the part size in bytes. The value must be in the range of 100 KB to 5 GB. The default value is 100 KB.
                uploadFileRequest.setPartSize(1 * 1024 * 1024);
                // Enable resumable upload. This feature is disabled by default.
                uploadFileRequest.setEnableCheckpoint(true);
                // The file that records the progress of a multipart upload. The upload progress is saved in this file. If a part fails to upload, the upload is resumed from the recorded breakpoint when you try again. After the upload is complete, this file is deleted.
                // If you do not set this parameter, the checkpoint file is stored in the same directory as the local file to upload and is named ${uploadFile}.ucp.
                uploadFileRequest.setCheckpointFile("yourCheckpointFile");
                // The metadata of the file.
                uploadFileRequest.setObjectMetadata(meta);
                // Set the upload callback. The parameter is of the Callback type.
                //uploadFileRequest.setCallback("yourCallbackEvent");

                // Perform a 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 the complete sample code for resumable upload, see GitHub example.