The content of this page has been automatically translated by AI. If you encounter any problems while reading, you can view the corresponding content in Chinese.

PUT Object

Last updated: 2026-05-21 15:01:29

Introduction

This document provides sample code and descriptions for uploading objects using the COS Android SDK. It covers three parts: advanced APIs, simple APIs, and multipart uploads.

Must-Knows

To use simple upload, you must have write permission for the target object: when an authorization policy is configured, the action must be set to cos:PutObject. For more authorization details, see CAM-supported business APIs.
To use multipart upload via the advanced API's automatic feature or the simple API, you must have permissions for the target object to initiate, upload parts for, and complete a multipart upload. When configuring an authorization policy, set the action to cos:InitiateMultipartUpload, cos:UploadPart, and cos:CompleteMultipartUpload. For more authorization details, see CAM-supported business APIs.
To use the resume-from-breakpoint feature of the advanced API, you must have permissions for the target object to initiate a multipart upload, upload parts, complete a multipart upload, list multipart upload tasks, and list uploaded parts. When configuring an authorization policy, set the action to cos:InitiateMultipartUpload, cos:UploadPart, cos:CompleteMultipartUpload, cos:ListMultipartUploads, and cos:ListParts. For more authorization details, see CAM-supported business APIs.

Related Examples

Feature Name
Description
Example code
Advanced interface
The advanced interface encapsulates the simple upload and multipart upload APIs, intelligently selects the upload method based on file size, and supports the resumable upload feature.
Simple interface
The PUT Object API can upload an object to a specified bucket, but does not support automatic multipart upload or resumable upload. It supports uploading objects up to 5 GB in size. For objects larger than 5 GB, use multipart upload or the advanced interface.
Multipart Operations
Multipart upload enables splitting an entire object into multiple parts and then uploading these parts to Cloud Object Storage (COS).

Advanced API (recommended)

The advanced API encapsulates simple upload and multipart upload APIs. It intelligently selects the upload method based on file size and also supports the resume-from-breakpoint feature.
Note:
The multipart threshold determines whether to use simple upload or multipart upload. This threshold is user-configurable and defaults to 2 MB.
The part size is user-configurable and defaults to 1 MB.
For file uploads, the advanced API selects simple upload for files smaller than the multipart threshold and multipart upload for files larger than the threshold.
For multipart uploads of files, the advanced API uploads multiple parts concurrently using multithreading.
For multipart uploads, the advanced upload API provides a feature to obtain the upload progress. For details, see the example below.
Streaming upload or binary upload supports only simple upload and does not support multipart upload.

Prerequisites: Creating a TransferManager

Before using the advanced API, you must first create a TransferManager instance. Before creating a TransferManager instance, you need to create a CosXmlService instance. For detailed code, see Create CosXmlService.
// Initialize TransferConfig with the default configuration. For customization, see the SDK API documentation.
// By default, files larger than or equal to 2 MB are automatically uploaded in parts, with a part size of 1 MB. You can modify the part threshold using the following code.
TransferConfig transferConfig = new TransferConfig.Builder() // Set the minimum object size for enabling multipart upload. The default is 2 MB. .setDivisionForUpload(2097152) // Set the part size for multipart upload. The default is 1 MB. .setSliceSizeForUpload(1048576) // Set whether to force simple upload and disable multipart upload. .setForceSimpleUpload(false) .build();
// Initialize TransferManager
TransferManager transferManager = new TransferManager(cosXmlService,
transferConfig);

Use Case: Uploading a Local File

// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // The object key, which is the identifier for the object's location in the bucket.
String srcPath = new File(context.getCacheDir(), "exampleobject.txt")
.toString(); // The absolute path of the local file.
// If an UploadId for initializing multipart upload exists, assign the corresponding uploadId value for resuming the upload. Otherwise, assign null.
String uploadId = null;

// Upload documents.
COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,
srcPath, uploadId);

// Set the callback for initializing multipart upload to obtain the uploadId. (Supported in version 5.9.7 and later.)
cosxmlUploadTask.setInitMultipleUploadListener(new InitMultipleUploadListener() {
@Override
public void onSuccess(InitiateMultipartUpload initiateMultipartUpload) {
// The uploadId for resuming the upload next time.
String uploadId = initiateMultipartUpload.uploadId;
}
});
// Set the upload progress callback.
cosxmlUploadTask.setCosXmlProgressListener(new CosXmlProgressListener() {
@Override
public void onProgress(long complete, long target) {
// todo Do something to update progress...
}
});
// Set the result callback.
cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =
(COSXMLUploadTask.COSXMLUploadTaskResult) result;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest request,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
// Set the task status callback to view the task progress.
cosxmlUploadTask.setTransferStateListener(new TransferStateListener() {
@Override
public void onStateChanged(TransferState state) {
// todo notify transfer state
}
});
Note:
After uploading, you can use the same Key to generate a file download link. For specific usage, see the Generate Pre-signed URL document. However, note that if your file has private read permission, the download link is valid only for a limited period.

Use Case: Uploading Binary Data

// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // The object key, which is the identifier for the object's location in the bucket.

// Upload a byte array.
byte[] bytes = "this is a test".getBytes(Charset.forName("UTF-8"));
COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,
bytes);

// Set the result callback.
cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =
(COSXMLUploadTask.COSXMLUploadTaskResult) result;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest request,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:
Uploading binary data supports only simple upload and does not support multipart upload.
After uploading, you can use the same Key to generate a file download link. For specific usage, see the Generate Pre-signed URL document. However, note that if your file has private read permission, the download link is valid only for a limited period.

Use Case: Streaming Upload

// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // The object key, which is the identifier for the object's location in the bucket.

// Stream upload
InputStream inputStream =
new ByteArrayInputStream("this is a test".getBytes(Charset.forName(
"UTF-8")));
COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,
inputStream);

// Set the result callback.
cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =
(COSXMLUploadTask.COSXMLUploadTaskResult) result;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest request,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:
Stream upload supports only simple upload and does not support multipart upload.
After uploading, you can use the same Key to generate a file download link. For specific usage, see the Generate Pre-signed URL document. However, note that if your file has private read permission, the download link is valid only for a limited period.

Use Case: Uploading via URI

// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // The object key, which is the identifier for the object's location in the bucket.

// The file's Uri
Uri uri = Uri.parse("exampleObject");
// If an UploadId for initializing multipart upload exists, assign the corresponding uploadId value for resuming the upload. Otherwise, assign null.
String uploadId = null;
COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,
uri, uploadId);

// Set the result callback.
cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =
(COSXMLUploadTask.COSXMLUploadTaskResult) result;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest request,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:
After uploading, you can use the same Key to generate a file download link. For specific usage, see the Generate Pre-signed URL document. However, note that if your file has private read permission, the download link is valid only for a limited period.

Use Case: Pausing, Resuming, and Canceling Uploads

To pause an upload task, you can use the following methods:
// If the final Complete Multipart Upload request has already been initiated during the upload, the pause operation will fail.
boolean pauseSuccess = cosxmlUploadTask.pauseSafely();
After pausing, you can resume the task using the following methods:
// If the pause is successful, you can resume the upload.
if (pauseSuccess) {
cosxmlUploadTask.resume();
}
You can also cancel the upload using the following methods:
cosxmlUploadTask.cancel();

Use Case: Batch Upload

// The absolute path of the local file.
File[] files = new File(context.getCacheDir(), "exampleDirectory").listFiles();

// Start batch upload.
for (File file : files) {
// If an UploadId for the initialized multipart upload exists, assign its value to resume the upload. Otherwise, assign null.
String uploadId = null;

// Upload documents.
COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,
file.getAbsolutePath(), uploadId);

// Set the result callback.
cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =
(COSXMLUploadTask.COSXMLUploadTaskResult) result;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest request,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
}

Use Case: Creating a Directory

// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
// The identifier for a folder's location in a bucket, also known as the object key, must end with '/'.
String cosPath = "exampleobject/";
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, new byte[0]);
cosXmlService.putObjectAsync(putObjectRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
PutObjectResult putObjectResult =
(PutObjectResult) result;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest request,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Setting Low-Priority Tasks

// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
// The identifier for a folder's location in a bucket, also known as the object key, must end with '/'.
String cosPath = "exampleobject/";
String srcPath = new File(context.getCacheDir(), "exampleobject")
.toString(); // The absolute path of the local file.
PutObjectRequest putObjectRequest= new PutObjectRequest(bucket, cosPath, srcPath);
putObjectRequest.setPriorityLow(); // Sets the upload task to low priority.
final COSXMLUploadTask uploadTask = transferManager.upload(putObjectRequest, null);
uploadTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {}


@Override
public void onFail(CosXmlRequest request, CosXmlClientException clientException, CosXmlServiceException serviceException) {}
});

Use Case: During Upload Adding Metadata

// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // The object key, which is the identifier for the object's location in the bucket.
String srcPath = new File(context.getCacheDir(), "exampleobject.txt")
.toString(); // The absolute path of the local file.
// If an UploadId for initializing multipart upload exists, assign the corresponding uploadId value for resuming the upload. Otherwise, assign null.
String uploadId = null;

// Upload files and add user-defined metadata to objects by setting request headers in the x-cos-meta-* format.
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket,cosPath,srcPath);
try {
putObjectRequest.setRequestHeaders("x-cos-meta-test", "testvalue", false);
} catch (CosXmlClientException e) {
e.printStackTrace();
}
COSXMLUploadTask cosxmlUploadTask = transferManager.upload(putObjectRequest, uploadId);

// Set the result callback.
cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =
(COSXMLUploadTask.COSXMLUploadTaskResult) result;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest request,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Simple Upload

The PUT Object API can upload an object to a specified bucket. This operation requires the requester to have WRITE permissions on the bucket. It supports uploading objects up to 5 GB in size. For objects larger than 5 GB, use multipart upload or the advanced API for upload.
Note:
The Key (file name) cannot end with /. Otherwise, it will be recognized as a folder.
Each root account (that is, the same APPID) can have a maximum of 1,000 bucket ACL rules. There is no limit on the number of object ACL rules. If you do not need object ACL control, do not configure it during upload. Objects will inherit bucket permissions by default.

Prerequisites: Creating a CosXmlService

Before calling a COS API, you must first create a CosXmlService instance. For detailed code, see Create CosXmlService.

Use Case: Uploading a Local File

// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // The object key, which is the identifier for the object's location in the bucket.
String srcPath = new File(context.getCacheDir(), "exampleobject.txt")
.toString(); // The absolute path of the local file.
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath,
srcPath);

putObjectRequest.setProgressListener(new CosXmlProgressListener() {
@Override
public void onProgress(long progress, long max) {
// todo Do something to update progress...
}
});
cosXmlService.putObjectAsync(putObjectRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
PutObjectResult putObjectResult = (PutObjectResult) result;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:
After uploading, you can use the same Key to generate a file download link. For specific usage, see the Generate Pre-signed URL document. However, note that if your file has private read permission, the download link is valid only for a limited period.

Use Case: Uploading Binary Data

// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // The object key, which is the identifier for the object's location in the bucket.
byte[] data = "this is a test".getBytes(Charset.forName("UTF-8"));
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath,
data);

putObjectRequest.setProgressListener(new CosXmlProgressListener() {
@Override
public void onProgress(long progress, long max) {
// todo Do something to update progress...
}
});
cosXmlService.putObjectAsync(putObjectRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
PutObjectResult putObjectResult = (PutObjectResult) result;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:
After uploading, you can use the same Key to generate a file download link. For specific usage, see the Generate Pre-signed URL document. However, note that if your file has private read permission, the download link is valid only for a limited period.

Use Case: Streaming Upload

// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // The object key, which is the identifier for the object's location in the bucket.
InputStream inputStream = new ByteArrayInputStream("this is a test".getBytes(
Charset.forName("UTF-8")));
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath,
inputStream);

putObjectRequest.setProgressListener(new CosXmlProgressListener() {
@Override
public void onProgress(long progress, long max) {
// todo Do something to update progress...
}
});
cosXmlService.putObjectAsync(putObjectRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
PutObjectResult putObjectResult = (PutObjectResult) result;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:
After uploading, you can use the same Key to generate a file download link. For specific usage, see the Generate Pre-signed URL document. However, note that if your file has private read permission, the download link is valid only for a limited period.

Multipart Operations

Multipart operations are used during large file uploads to mitigate various interruption issues that may arise from the extended time required for uploading large files. For more information on multipart uploads, see Multipart Upload. The operation flow for multipart upload objects is as follows.

Operation Process

Multipart Upload Process

1. Initialize a multipart upload (Initiate Multipart Upload) to obtain the UploadId.
2. Upload parts (Upload Part) using the UploadId, or copy parts (Upload Part Copy).
3. Complete the multipart upload (Complete Multipart Upload).

Multipart Upload Process

1. If the UploadId is not recorded, query the multipart upload tasks (List Multipart Uploads) to obtain the UploadId for the corresponding file.
2. List the uploaded parts (List Parts) using the UploadId.
3. Upload the remaining parts (Upload Part) using the UploadId, or copy the remaining parts (Upload Part Copy).
4. Complete the multipart upload (Complete Multipart Upload).

Terminating a Multipart Upload

1. If the UploadId is not recorded, query the multipart upload tasks (List Multipart Uploads) to obtain the UploadId for the corresponding file.
2. Abort the multipart upload and delete the uploaded parts (Abort Multipart Upload).

Prerequisites: Creating a CosXmlService

Before calling a COS API, you must first create a CosXmlService instance. For detailed code, see Create CosXmlService.

Use Case: Querying Multipart Uploads

Query the multipart uploads in progress in the specified bucket (List Multipart Uploads).
// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
ListMultiUploadsRequest listMultiUploadsRequest =
new ListMultiUploadsRequest(bucket);
cosXmlService.listMultiUploadsAsync(listMultiUploadsRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
ListMultiUploadsResult listMultiUploadsResult =
(ListMultiUploadsResult) result;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Initiating a Multipart Upload

Initialize the Multipart Upload upload operation and obtain the corresponding uploadId (Initiate Multipart Upload).
// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // The object key, which is the identifier for the object's location in the bucket.

InitMultipartUploadRequest initMultipartUploadRequest =
new InitMultipartUploadRequest(bucket, cosPath);
cosXmlService.initMultipartUploadAsync(initMultipartUploadRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
// The uploadId for the multipart upload.
uploadId = ((InitMultipartUploadResult) result)
.initMultipartUpload.uploadId;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Uploading Parts

Objects uploaded in mulitparts.
// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // The object key, which is the identifier for the object's location in the bucket.
UploadPartRequest uploadPartRequest = new UploadPartRequest(bucket, cosPath,
partNumber, srcFile.getPath(), offset, PART_SIZE, uploadId);

uploadPartRequest.setProgressListener(new CosXmlProgressListener() {
@Override
public void onProgress(long progress, long max) {
// todo Do something to update progress...
}
});

cosXmlService.uploadPartAsync(uploadPartRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
String eTag = ((UploadPartResult) result).eTag;
eTags.put(partNumber, eTag);
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Listing Uploaded Parts

Query the uploaded mulitparts in a specific multiple parts uploading (List Parts).
// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // The object key, which is the identifier for the object's location in the bucket.

ListPartsRequest listPartsRequest = new ListPartsRequest(bucket, cosPath,
uploadId);
cosXmlService.listPartsAsync(listPartsRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
ListParts listParts = ((ListPartsResult) result).listParts;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Completing a Multipart Upload

Complete multipart upload of the entire file.
// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // The object key, which is the identifier for the object's location in the bucket.

CompleteMultiUploadRequest completeMultiUploadRequest =
new CompleteMultiUploadRequest(bucket,
cosPath, uploadId, eTags);
cosXmlService.completeMultiUploadAsync(completeMultiUploadRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
CompleteMultiUploadResult completeMultiUploadResult =
(CompleteMultiUploadResult) result;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Terminating a Multipart Upload

Abort a mulitpart uploading operation and delete the uploaded parts.
// The bucket name consists of bucketname-appid. You must provide the appid. You can view the bucket name in the COS console. https://console.cloud.tencent.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // The object key, which is the identifier for the object's location in the bucket.

AbortMultiUploadRequest abortMultiUploadRequest =
new AbortMultiUploadRequest(bucket,
cosPath, uploadId);
cosXmlService.abortMultiUploadAsync(abortMultiUploadRequest,
new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
AbortMultiUploadResult abortMultiUploadResult =
(AbortMultiUploadResult) result;
}

// If you use kotlin to make the call, note that the exception in the callback method is nullable. Otherwise, the onFail method will not be called back, which means:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:
For more complete examples, go to GitHub.

SDK API Reference

For detailed parameters and method descriptions of all SDK APIs, see SDK API Reference.

API Operations

For API documentation on simple operations, see the PUT Object document.
For API documentation on multipart operations, see the List Multipart Uploads, Initiate Multipart Upload, Upload Part, List Parts, Abort Multipart Upload, and Complete Multipart Upload documents.