All Products
Search
Document Center

Object Storage Service:Multipart upload (Java SDK)

Last Updated:Jul 30, 2025

Object Storage Service (OSS) provides the multipart upload feature. Multipart upload allows you to split a large object into multiple parts to upload. After these parts are uploaded, you can call the CompleteMultipartUpload operation to combine the parts into a complete object.

Precautions

  • 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 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.

  • You must have the oss:PutObject permission to complete the multipart upload process, which includes the InitiateMultipartUpload, UploadPart, and CompleteMultipartUpload operations. For more information, see Attach a custom policy to a RAM user.

Multipart upload process

Multipart upload consists of the following three steps:

  1. Initiate a multipart upload.

    Call the ossClient.initiateMultipartUpload method. OSS creates and returns a globally unique uploadId.

  2. Upload the parts.

    Call the ossClient.uploadPart method to upload the data for each part.

    Note
    • For the same uploadId, the part number (PartNumber) identifies the position of the part relative to the entire file. If you use the same part number to upload new data, the existing data for that part in OSS is overwritten.

    • OSS includes the MD5 hash of the received part data in the ETag header and returns it to you.

    • OSS calculates the MD5 hash of the uploaded data and compares it with the MD5 hash calculated by the SDK. If the hashes do not match, the InvalidDigest error code is returned.

  3. Complete the multipart upload.

    After you upload all the parts, call the ossClient.completeMultipartUpload method to combine the parts into a complete file.

Sample code

The following sample code provides an example on how to implement a multipart upload task by following the multipart upload process:

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.internal.Mimetypes; import com.aliyun.oss.model.*; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class Demo { public static void main(String[] args) throws Exception { // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name, such as examplebucket. String bucketName = "examplebucket"; // Specify the full path of the object, such as exampledir/exampleobject.txt. The full path cannot contain the bucket name. String objectName = "exampledir/exampleobject.txt"; // The path of the local file to be uploaded. String filePath = "D:\\localpath\\examplefile.txt"; // Specify the region in which the bucket is located. In this example, the China (Hangzhou) region is used. Set Region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, 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 { // Create an InitiateMultipartUploadRequest object. InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectName); // Create an ObjectMetadata object and set the Content-Type. ObjectMetadata metadata = new ObjectMetadata(); if (metadata.getContentType() == null) { metadata.setContentType(Mimetypes.getInstance().getMimetype(new File(filePath), objectName)); } System.out.println("Content-Type: " + metadata.getContentType()); // Bind the metadata to the upload request. request.setObjectMetadata(metadata); // Initialize the multipart upload. InitiateMultipartUploadResult upresult = ossClient.initiateMultipartUpload(request); // Return the uploadId. String uploadId = upresult.getUploadId(); // partETags is a collection of PartETag objects. A PartETag consists of the ETag and part number of a part. List<PartETag> partETags = new ArrayList<PartETag>(); // The size of each part. This is used to calculate the number of parts. Unit: bytes. // The minimum part size is 100 KB and the maximum part size is 5 GB. The size of the last part can be less than 100 KB. // Set the part size to 1 MB. final long partSize = 1 * 1024 * 1024L; // Calculate the number of parts based on the size of the data to be uploaded. The following code uses a local file as an example to show how to obtain the size of the data to be uploaded using File.length(). final File sampleFile = new File(filePath); long fileLength = sampleFile.length(); int partCount = (int) (fileLength / partSize); if (fileLength % partSize != 0) { partCount++; } // Traverse and upload parts. for (int i = 0; i < partCount; i++) { long startPos = i * partSize; long curPartSize = (i + 1 == partCount) ? (fileLength - startPos) : partSize; UploadPartRequest uploadPartRequest = new UploadPartRequest(); uploadPartRequest.setBucketName(bucketName); uploadPartRequest.setKey(objectName); uploadPartRequest.setUploadId(uploadId); // Set the stream of the part to be uploaded. // The following code uses a local file as an example to show how to create a FileInputStream and skip the specified data using the InputStream.skip() method. InputStream instream = new FileInputStream(sampleFile); instream.skip(startPos); uploadPartRequest.setInputStream(instream); // Set the part size. uploadPartRequest.setPartSize(curPartSize); // Set the part number. Each uploaded part has a part number that ranges from 1 to 10,000. If the part number is not within this range, OSS returns the InvalidArgument error code. uploadPartRequest.setPartNumber(i + 1); // Parts do not need to be uploaded in sequence. They can even be uploaded from different clients. OSS combines the parts into a complete file based on their part numbers in ascending order. UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest); // After each part is uploaded, the result returned by OSS contains a PartETag. The PartETag is saved in partETags. partETags.add(uploadPartResult.getPartETag()); // Close the stream. instream.close(); } // Create a CompleteMultipartUploadRequest object. // When you complete the multipart upload, you must provide all valid partETags. After OSS receives the submitted partETags, it verifies the validity of each part. After all parts are verified, OSS combines these parts into a complete file. CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, objectName, uploadId, partETags); // Complete the multipart upload. CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest); System.out.println("Upload successful. ETag: " + completeMultipartUploadResult.getETag()); } 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 (ClientException ce) { System.out.println("Caught a 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 { if (ossClient != null) { ossClient.shutdown(); } } } }

Common scenarios

Set metadata when you initialize a multipart upload

The following code snippet shows how to set metadata when you initialize a multipart upload.

// Create an InitiateMultipartUploadRequest object. InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectName); ObjectMetadata metadata = new ObjectMetadata(); metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString()); // Specify the caching behavior of the web page for the object. metadata.setCacheControl("no-cache"); // Specify the name of the object when the object is downloaded. metadata.setContentDisposition("attachment;filename=oss_MultipartUpload.txt"); // Specify the content encoding format of the object. metadata.setContentEncoding(OSSConstants.DEFAULT_CHARSET_NAME); // Specify whether to overwrite an object that has the same name when you initialize the multipart upload. In this example, this parameter is set to true, which indicates that the object that has the same name is not overwritten. metadata.setHeader("x-oss-forbid-overwrite", "true"); // Specify the server-side encryption method used to encrypt each part of the object to be uploaded. metadata.setHeader(OSSHeaders.OSS_SERVER_SIDE_ENCRYPTION, ObjectMetadata.KMS_SERVER_SIDE_ENCRYPTION); // Specify the encryption algorithm of the object. If this parameter is not specified, the AES256 algorithm is used to encrypt the object. metadata.setHeader(OSSHeaders.OSS_SERVER_SIDE_DATA_ENCRYPTION, ObjectMetadata.KMS_SERVER_SIDE_ENCRYPTION); // Specify the customer master key (CMK) managed by KMS. metadata.setHeader(OSSHeaders.OSS_SERVER_SIDE_ENCRYPTION_KEY_ID, "9468da86-3509-4f8d-a61e-6eab1eac****"); // Specify the storage class of the object. metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard); // Specify tags for the object. You can specify multiple tags at a time. metadata.setHeader(OSSHeaders.OSS_TAGGING, "a:1"); request.setObjectMetadata(metadata); // Automatically set ContentType based on the file. If this parameter is not set, the default value of ContentType is application/octet-stream. if (metadata.getContentType() == null) { metadata.setContentType(Mimetypes.getInstance().getMimetype(new File(filePath), objectName)); } // Bind the metadata to the upload request. request.setObjectMetadata(metadata); // Initialize the multipart upload. InitiateMultipartUploadResult upresult = ossClient.initiateMultipartUpload(request);

Set file access permissions when you complete a multipart upload

The following sample code shows how to set the access permissions on a file when you complete a multipart upload.

completeMultipartUploadRequest.setObjectACL(CannedAccessControlList.Private);

Automatically process the ETags of parts when you complete a multipart upload

The following sample code shows how to automatically process the ETags of parts when you complete a multipart upload.

// Specify whether to list all parts that are uploaded for the current UploadId. You can set partETags in CompleteMultipartUploadRequest to null to merge a complete file by listing part data on the server only in Java SDK 3.14.0 and later. Map<String, String> headers = new HashMap<String, String>(); // If you specify x-oss-complete-all:yes, OSS lists all parts that are uploaded for the current UploadId, sorts the parts by part number in ascending order, and then runs the CompleteMultipartUpload operation. // If you specify x-oss-complete-all:yes, you cannot specify the body. Otherwise, an error is reported. headers.put("x-oss-complete-all","yes"); completeMultipartUploadRequest.setHeaders(headers);

Cancel a multipart upload event

To cancel a multipart upload event, you must obtain the uploadId after you call InitiateMultipartUpload. Then, call the abortMultipartUpload method and pass the uploadId to cancel the event. After a multipart upload event is canceled, you can no longer use the uploadId for any operations, and the uploaded part data is deleted. The following sample code shows how to cancel a multipart upload event.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name, such as examplebucket. String bucketName = "examplebucket"; // Specify the full path of the object, such as exampledir/exampleobject.txt. The full path cannot contain the bucket name. String objectName = "exampledir/exampleobject.txt"; // Specify the upload ID, such as 0004B999EF518A1FE585B0C9360D****. The upload ID is obtained from the result returned after you call InitiateMultipartUpload to initialize the multipart upload. String uploadId = "0004B999EF518A1FE585B0C9360D****"; // Specify the region in which the bucket is located. In this example, the China (Hangzhou) region is used. Set Region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, 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 { // Cancel the multipart upload. AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, objectName, uploadId); ossClient.abortMultipartUpload(abortMultipartUploadRequest); } 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 (ClientException 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 { if (ossClient != null) { ossClient.shutdown(); } } } }

List uploaded parts

To list uploaded parts, you must obtain the uploadId after you call InitiateMultipartUpload and before you call CompleteMultipartUpload. Then, you can call the listParts method and pass the uploadId to list all parts that have been successfully uploaded for that ID.

List uploaded parts in a simple way

The following code shows how to list uploaded parts.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name, such as examplebucket. String bucketName = "examplebucket"; // Specify the full path of the object, such as exampledir/exampleobject.txt. The full path cannot contain the bucket name. String objectName = "exampledir/exampleobject.txt"; // Specify the upload ID, such as 0004B999EF518A1FE585B0C9360D****. The upload ID is obtained from the result returned after you call InitiateMultipartUpload to initialize the multipart upload and before you call CompleteMultipartUpload to complete the multipart upload. String uploadId = "0004B999EF518A1FE585B0C9360D****"; // Specify the region in which the bucket is located. In this example, the China (Hangzhou) region is used. Set Region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, 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 { // List the uploaded parts. ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, objectName, uploadId); // Set the uploadId. //listPartsRequest.setUploadId(uploadId); // Set the number of parts on each page to 100 for paging. By default, 1,000 parts are listed. listPartsRequest.setMaxParts(100); // Specify the start position of the list. Only parts whose part numbers are greater than the value of this parameter are listed. listPartsRequest.setPartNumberMarker(2); PartListing partListing = ossClient.listParts(listPartsRequest); for (PartSummary part : partListing.getParts()) { // Obtain the part number. System.out.println(part.getPartNumber()); // Obtain the part data size. System.out.println(part.getSize()); // Obtain the ETag of the part. System.out.println(part.getETag()); // Obtain the last modified time of the part. System.out.println(part.getLastModified()); } } 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 (ClientException 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 { if (ossClient != null) { ossClient.shutdown(); } } } }

List all uploaded parts

By default, listParts can list a maximum of 1,000 parts at a time. If the number of parts is greater than 1,000, use the following code to list all uploaded parts.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name, such as examplebucket. String bucketName = "examplebucket"; // Specify the full path of the object, such as exampledir/exampleobject.txt. The full path cannot contain the bucket name. String objectName = "exampledir/exampleobject.txt"; // Specify the upload ID, such as 0004B999EF518A1FE585B0C9360D****. The upload ID is obtained from the result returned after you call InitiateMultipartUpload to initialize the multipart upload and before you call CompleteMultipartUpload to complete the multipart upload. String uploadId = "0004B999EF518A1FE585B0C9360D****"; // Specify the region in which the bucket is located. In this example, the China (Hangzhou) region is used. Set Region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, 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 { // List all uploaded parts. PartListing partListing; ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, objectName, uploadId); do { partListing = ossClient.listParts(listPartsRequest); for (PartSummary part : partListing.getParts()) { // Obtain the part number. System.out.println(part.getPartNumber()); // Obtain the part data size. System.out.println(part.getSize()); // Obtain the ETag of the part. System.out.println(part.getETag()); // Obtain the last modified time of the part. System.out.println(part.getLastModified()); } // Specify the start position of the list. Only parts whose part numbers are greater than the value of this parameter are listed. listPartsRequest.setPartNumberMarker(partListing.getNextPartNumberMarker()); } while (partListing.isTruncated()); } 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 (ClientException 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 { if (ossClient != null) { ossClient.shutdown(); } } } }

List all uploaded parts by page

The following code shows how to specify the number of parts on each page and list all parts by page.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name, such as examplebucket. String bucketName = "examplebucket"; // Specify the full path of the object, such as exampledir/exampleobject.txt. The full path cannot contain the bucket name. String objectName = "exampledir/exampleobject.txt"; // Specify the upload ID, such as 0004B999EF518A1FE585B0C9360D****. The upload ID is obtained from the result returned after you call InitiateMultipartUpload to initialize the multipart upload and before you call CompleteMultipartUpload to complete the multipart upload. String uploadId = "0004B999EF518A1FE585B0C9360D****"; // Specify the region in which the bucket is located. In this example, the China (Hangzhou) region is used. Set Region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, 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 { // List uploaded parts by page. PartListing partListing; ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, objectName, uploadId); // Set the number of parts to list on each page to 100 for paging. listPartsRequest.setMaxParts(100); do { partListing = ossClient.listParts(listPartsRequest); for (PartSummary part : partListing.getParts()) { // Obtain the part number. System.out.println(part.getPartNumber()); // Obtain the part data size. System.out.println(part.getSize()); // Obtain the ETag of the part. System.out.println(part.getETag()); // Obtain the last modified time of the part. System.out.println(part.getLastModified()); } listPartsRequest.setPartNumberMarker(partListing.getNextPartNumberMarker()); } while (partListing.isTruncated()); } 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 (ClientException 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 { if (ossClient != null) { ossClient.shutdown(); } } } } 

List multipart upload events

You can call the listMultipartUploads method to list all ongoing multipart upload events. Ongoing multipart upload events are events that have been initialized but not yet completed or canceled.

Parameter

Description

Method

prefix

The prefix that the names of returned files must contain. Note that if you use the prefix to perform a query, the names of the returned files still contain the prefix.

ListMultipartUploadsRequest.setPrefix(String prefix)

delimiter

A character that is used to group file names. All file names that contain the specified prefix and appear before the first occurrence of the delimiter are grouped as a single element.

ListMultipartUploadsRequest.setDelimiter(String delimiter)

maxUploads

The maximum number of multipart upload events to return. The default value and the maximum value are both 1000.

ListMultipartUploadsRequest.setMaxUploads(Integer maxUploads)

keyMarker

The multipart upload events for which the lexicographical order of the file names is greater than the value of the keyMarker parameter. This parameter is used together with the uploadIdMarker parameter to specify the start position of the returned results.

ListMultipartUploadsRequest.setKeyMarker(String keyMarker)

uploadIdMarker

This parameter is used together with the keyMarker parameter to specify the start position of the returned results. If the keyMarker parameter is not set, this parameter is invalid. If the keyMarker parameter is set, the query results include the following:

  • All files whose names are lexicographically greater than the value of the keyMarker parameter.

  • All multipart upload events for which the file names are the same as the value of the keyMarker parameter and the uploadIds are greater than the value of the uploadIdMarker parameter.

ListMultipartUploadsRequest.setUploadIdMarker(String uploadIdMarker)

List multipart upload events in a simple way

The following code shows how to list multipart upload events.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name, such as examplebucket. String bucketName = "examplebucket"; // Specify the region in which the bucket is located. In this example, the China (Hangzhou) region is used. Set Region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, 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 { // List multipart upload events. By default, 1,000 parts are listed. ListMultipartUploadsRequest listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName); MultipartUploadListing multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest); for (MultipartUpload multipartUpload : multipartUploadListing.getMultipartUploads()) { // Obtain the uploadId. System.out.println(multipartUpload.getUploadId()); // Obtain the key. System.out.println(multipartUpload.getKey()); // Obtain the initialization time of the multipart upload. System.out.println(multipartUpload.getInitiated()); } } 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 (ClientException 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 { if (ossClient != null) { ossClient.shutdown(); } } } }

If isTruncated in the result is true, nextKeyMarker and nextUploadIdMarker are returned to indicate the starting positions for the next read. If you cannot retrieve all upload events in a single request, you can list them by page.

List all multipart upload events

By default, listMultipartUploads can list a maximum of 1,000 events at a time. If the number of events is greater than 1,000, use the following code to list all multipart upload events.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name, such as examplebucket. String bucketName = "examplebucket"; // Specify the region in which the bucket is located. In this example, the China (Hangzhou) region is used. Set Region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, 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 { // List multipart upload events. MultipartUploadListing multipartUploadListing; ListMultipartUploadsRequest listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName); do { multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest); for (MultipartUpload multipartUpload : multipartUploadListing.getMultipartUploads()) { // Obtain the uploadId. System.out.println(multipartUpload.getUploadId()); // Obtain the file name. System.out.println(multipartUpload.getKey()); // Obtain the initialization time of the multipart upload. System.out.println(multipartUpload.getInitiated()); } listMultipartUploadsRequest.setKeyMarker(multipartUploadListing.getNextKeyMarker()); listMultipartUploadsRequest.setUploadIdMarker(multipartUploadListing.getNextUploadIdMarker()); } while (multipartUploadListing.isTruncated()); } 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 (ClientException 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 { if (ossClient != null) { ossClient.shutdown(); } } } }

List all upload events by page

The following code shows how to list all upload events by page.

import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.*; public class Demo { public static void main(String[] args) throws Exception { // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name, such as examplebucket. String bucketName = "examplebucket"; // Specify the region in which the bucket is located. In this example, the China (Hangzhou) region is used. Set Region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer used, 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 { // List multipart upload events. MultipartUploadListing multipartUploadListing; ListMultipartUploadsRequest listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName); // Set the number of multipart upload events to list on each page. listMultipartUploadsRequest.setMaxUploads(50); do { multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest); for (MultipartUpload multipartUpload : multipartUploadListing.getMultipartUploads()) { // Obtain the uploadId. System.out.println(multipartUpload.getUploadId()); // Obtain the key. System.out.println(multipartUpload.getKey()); // Obtain the initialization time of the multipart upload. System.out.println(multipartUpload.getInitiated()); } listMultipartUploadsRequest.setKeyMarker(multipartUploadListing.getNextKeyMarker()); listMultipartUploadsRequest.setUploadIdMarker(multipartUploadListing.getNextUploadIdMarker()); } while (multipartUploadListing.isTruncated()); } 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 (ClientException 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 { if (ossClient != null) { ossClient.shutdown(); } } } }

Perform multipart upload for a network stream or data stream

This topic describes how to perform a multipart upload for a local file. To perform a multipart upload for a network stream or data stream, see Multipart upload for data streams.

References