tencent cloud

PUT Object
Last updated:2026-02-27 15:21:58
PUT Object
Last updated: 2026-02-27 15:21:58

Introduction

This article introduces the sample code and description for implementing object upload via COS Android SDK, including three parts: advanced APIs, simple APIs, and multipart upload.

Must-Knows

If you use simple upload, you need write permission for the target object: when configuring authorization policies, set the action to cos:PutObject. For more authorization details, see CAM-Supported Service APIs.
If you use the automatic multipart upload of the advanced API or implement multipart upload using the simple API, you need permissions for initiating multipart upload, uploading parts, and completing multipart upload for the target object: when configuring authorization policies, set the action to cos:InitiateMultipartUpload, cos:UploadPart, cos:CompleteMultipartUpload. For more authorization details, see CAM-Supported Service APIs.
If you use the resumable upload of the advanced API, you need permissions for initiating multipart upload, uploading parts, completing multipart upload, listing multipart upload tasks, and listing uploaded parts for the target object: when configuring authorization policies, set the action to cos:InitiateMultipartUpload, cos:UploadPart, cos:CompleteMultipartUpload, cos:ListMultipartUploads, cos:ListParts. For more authorization details, see CAM-Supported Service APIs.

Related Examples

Function Name
Description
Example code
Advanced APIs
Advanced APIs encapsulate simple upload and multipart upload APIs, intelligently selecting the upload method based on file size, and also support the resumable upload feature.
Simple APIs
The PUT Object API allows you to upload an object to a specified bucket, but it does not support automatic multipart upload or resumable upload. It supports uploading objects up to 5GB in size. For objects larger than 5GB, please use multipart upload or Advanced APIs.
Multipart Operations
Multipart upload allows you to split an entire object into multiple parts and then upload these parts to Cloud Object Storage (COS).

Advanced API (recommended)

Advanced APIs encapsulate the simple upload and multipart upload APIs, intelligently select the upload method based on file size, and also support the resumable upload feature.
Note:
The multipart threshold is used to determine whether to use simple upload or multipart upload. The threshold can be customized by users and defaults to 2MB.
The multipart size can be customized by users, defaulting to 1MB.
The Advanced APIs select simple upload for files below the multipart threshold and multipart upload for files above it.
For multipart uploads of file types, the Advanced APIs will upload multiple parts simultaneously with multi-threaded concurrency.
For multipart uploads, the Advanced upload APIs provide the feature to retrieve progress, as shown in the sample below.
Streaming upload or binary upload only supports simple upload and does not support multipart upload.

Preparation: Creating TransferManager

Before operations are performed using the Advanced APIs, you must create an instance of TransferManager. Prior to creating a TransferManager instance, you need to create a CosXmlService instance. For detailed code, see Create CosXmlService.
// Initialize TransferConfig using the default configuration here. If customization is needed, see the SDK API documentation.
// By default, files larger than or equal to 2MB automatically use multipart upload with a part size of 1MB. The multipart threshold can be modified with the following code
TransferConfig transferConfig = new TransferConfig.Builder() // Set the minimum object size to enable multipart upload. Default: 2MB .setDivisionForUpload(2097152) // Set the part size during multipart upload. Default: 1MB .setSliceSizeForUpload(1048576) // Set whether to force simple upload and disable multipart upload .setForceSimpleUpload(false) .build();
// Initialize TransferManager
TransferManager transferManager = new TransferManager(cosXmlService,
transferConfig);

Use Cases: Upload Local Files

// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object in the bucket
String srcPath = new File(context.getCacheDir(), "exampleobject.txt")
.toString(); // absolute path of the local file
// If an UploadId for initializing multipart upload exists, assign the corresponding uploadId value to resume 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) {
// uploadId used 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 return result callback
cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =
(COSXMLUploadTask.COSXMLUploadTaskResult) result;
}

// If you are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type 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 upload, you can use the same Key to generate a file download link. For specific usage, refer to the Generate Presigned URL document. But note that if your file has private read permissions, the download link has a limited validity period.

Use Case: Upload Binary Data

// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object in the bucket

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

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

// If you are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type 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 only supports simple upload and does not support multipart upload.
After upload, you can use the same Key to generate a file download link. For specific usage, refer to the Generate Presigned URL document. But note that if your file has private read permissions, the download link has a limited validity period.

Use Cases: Streaming Upload

// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object in the bucket

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

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

// If you are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type CosXmlServiceException?
@Override
public void onFail(CosXmlRequest request,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:
Streaming upload only supports simple upload and does not support multipart upload.
After upload, you can use the same Key to generate a file download link. For specific usage, refer to the Generate Presigned URL document. But note that if your file has private read permissions, the download link has a limited validity period.

Use Case: Upload via URI

// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object 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 to resume the upload; otherwise, assign null
String uploadId = null;
COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,
uri, uploadId);

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

// If you are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type CosXmlServiceException?
@Override
public void onFail(CosXmlRequest request,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:
After upload, you can use the same Key to generate a file download link. For specific usage, refer to the Generate Presigned URL document. But note that if your file has private read permissions, the download link has a limited validity period.

Use Cases: Upload Pause, Resume, and Cancel

For an upload task, you can pause it in the following ways:
// If the final Complete Multipart Upload request has been initiated during the upload process, pausing will fail
boolean pauseSuccess = cosxmlUploadTask.pauseSafely();
After pausing, you can resume the upload in the following ways:
// If paused successfully, the upload can be resumed
if (pauseSuccess) {
cosxmlUploadTask.resume();
}
You can also cancel an upload in the following ways:
cosxmlUploadTask.cancel();

Use Case: Batch Upload

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

// Start batch upload
for (File file : files) {
// If there is an UploadId for an initialized multipart upload, assign the corresponding uploadId value for resuming 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 are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type CosXmlServiceException?
@Override
public void onFail(CosXmlRequest request,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
}

Use Case: Create Directory

// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
// The folder path identifier in the bucket, referred to 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 are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type CosXmlServiceException?
@Override
public void onFail(CosXmlRequest request,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Set Low-Priority Tasks

// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
// The folder path identifier in the bucket, referred to as the object key, must end with '/'
String cosPath = "exampleobject/";
String srcPath = new File(context.getCacheDir(), "exampleobject")
.toString(); // absolute path of the local file
PutObjectRequest putObjectRequest= new PutObjectRequest(bucket, cosPath, srcPath);
putObjectRequest.setPriorityLow(); // This is set as a low priority upload task
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: When Uploading Add Metadata

// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object in the bucket
String srcPath = new File(context.getCacheDir(), "exampleobject.txt")
.toString(); // absolute path of the local file
// If an UploadId for initializing multipart upload exists, assign the corresponding uploadId value to resume the upload; otherwise, assign null
String uploadId = null;

// Upload a file by setting request headers in the x-cos-meta-* format to add user-defined metadata to the object.
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 return result callback
cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =
(COSXMLUploadTask.COSXMLUploadTaskResult) result;
}

// If you are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type 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 interface can upload an object to a specified bucket. This operation requires the requester to have WRITE permission on the bucket. It supports uploading objects up to 5GB in size. For objects larger than 5GB, please use multipart upload or advanced APIs.
Note:
Key (filename) cannot end with /, otherwise it will be recognized as a folder.
Each primary account (that is, the same APPID) has a limit of 1000 ACL rules for buckets, while there is no limit on the number of object ACL rules. If you do not require object ACL control, do not configure it during upload; objects will inherit the bucket permissions by default.

Preliminary Preparation: Create CosXmlService

Before calling the COS API, you must create an instance of CosXmlService. For detailed code, see Create CosXmlService.

Use Cases: Upload Local Files

// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object in the bucket.
String srcPath = new File(context.getCacheDir(), "exampleobject.txt")
.toString(); // 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 are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:
After upload, you can use the same Key to generate a file download link. For specific usage, refer to the Generate Presigned URL document. But note that if your file has private read permissions, the download link has a limited validity period.

Use Case: Upload Binary Data

// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object 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 are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:
After upload, you can use the same Key to generate a file download link. For specific usage, refer to the Generate Presigned URL document. But note that if your file has private read permissions, the download link has a limited validity period.

Use Cases: Streaming Upload

// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object 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 are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});
Note:
After upload, you can use the same Key to generate a file download link. For specific usage, refer to the Generate Presigned URL document. But note that if your file has private read permissions, the download link has a limited validity period.

Multipart Operations

Multipart operations involve using a series of parts during large file uploads to mitigate various interruption issues that may arise due to prolonged upload durations. For more information on multipart uploads, see Multipart Upload. The workflow for uploading objects via multipart upload is as follows.

Operation Process

Multipart Upload Process

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

Multipart Resume Upload Process

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

Terminate Multipart Upload Process

1. If no UploadId is 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).

Preliminary Preparation: Create CosXmlService

Before calling the COS API, you must create an instance of CosXmlService. For detailed code, see Create CosXmlService.

Use Case: Query Multipart Uploads

Query the multipart uploads in progress in the specified bucket (List Multipart Uploads).
// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.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 are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Initialize Multipart Upload

Initialize the Multipart Upload upload operation and obtain the corresponding uploadId (Initiate Multipart Upload).
// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object 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 multipart upload.
uploadId = ((InitMultipartUploadResult) result)
.initMultipartUpload.uploadId;
}

// If you are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type 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 is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // the location identifier of the object in the bucket, i.e., the object key
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 are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Query Uploaded Parts

Query the uploaded mulitparts in a specific multiple parts uploading (List Parts).
// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object 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 are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Complete Multipart Upload

Complete multipart upload of the entire file.
// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object 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 are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Abort Multipart Upload

Abort a mulitpart uploading operation and delete the uploaded parts.
// The bucket name is composed of bucketname-appid. The appid must be included. You can view the bucket name in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object 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 are using the kotlin language to make the call, note that the exception in the callback method is nullable; otherwise, the onFail method will not be triggered, that is:
// clientException is of type CosXmlClientException?, and serviceException is of type 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 samples, please visit GitHub.

SDK API Reference

For detailed parameters and methods of all APIs in the SDK, see SDK API Reference.

API Operations

For the API documentation on simple operations, see the PUT Object document.
For the 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.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback