API | Operation | Description |
Uploading an object | Uploads an object to a bucket. |
API | Operation | Description |
Querying multipart uploads | Queries in-progress multipart uploads. | |
Initializing a multipart upload operation | Initializes a multipart upload operation. | |
Uploading parts | Uploads an object in multiple parts. | |
Querying uploaded parts | Queries the uploaded parts of a multipart upload. | |
Completing a multipart upload | Completes the multipart upload of a file. | |
Aborting a multipart upload | Aborts a multipart upload and deletes the uploaded parts. |
// Initialize TransferConfig. The default configuration is used here. To customize the configuration, please see the SDK API documentation.TransferConfig transferConfig = new TransferConfig.Builder().build();// Initialize TransferManagerTransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // Location identifier of the object in the bucket, i.e., the object keyString srcPath = new File(context.getCacheDir(), "exampleobject").toString(); // Absolute path of the local file// If there is an uploadId for the initialized multipart upload, assign the value of uploadId here to resume the upload. Otherwise, assign nullString uploadId = null;// Upload the fileCOSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,srcPath, uploadId);// Set the callback for initializing multipart upload (supported starting from v5.9.7)cosxmlUploadTask.setInitMultipleUploadListener(new InitMultipleUploadListener() {@Overridepublic void onSuccess(InitiateMultipartUpload initiateMultipartUpload) {// The `uploadId` used for next uploadString uploadId = initiateMultipartUpload.uploadId;}});// Set the upload progress callbackcosxmlUploadTask.setCosXmlProgressListener(new CosXmlProgressListener() {@Overridepublic void onProgress(long complete, long target) {// todo Do something to update progress...}});// Set the response callbackcosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =(COSXMLUploadTask.COSXMLUploadTaskResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});// Set the job status callback to view the job progresscosxmlUploadTask.setTransferStateListener(new TransferStateListener() {@Overridepublic void onStateChanged(TransferState state) {// todo notify transfer state}});
TransferConfig transferConfig = new TransferConfig.Builder().build();TransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // Location identifier of the object in the bucket, i.e., the object key// Upload a byte arraybyte[] bytes = "this is a test".getBytes(Charset.forName("UTF-8"));COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,bytes);// Set the response callbackcosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =(COSXMLUploadTask.COSXMLUploadTaskResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
TransferConfig transferConfig = new TransferConfig.Builder().build();TransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // Location identifier of the object in the bucket, i.e., the object key// Stream uploadInputStream inputStream =new ByteArrayInputStream("this is a test".getBytes(Charset.forName("UTF-8")));COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,inputStream);// Set the response callbackcosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =(COSXMLUploadTask.COSXMLUploadTaskResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
TransferConfig transferConfig = new TransferConfig.Builder().build();TransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // Location identifier of the object in the bucket, i.e., the object key// URI of the fileUri uri = Uri.parse("exampleObject");// If there is an `uploadId` for an initialized multipart upload, assign the value of the `uploadId` here to resume the upload; otherwise, assign `null`String uploadId = null;COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,uri, uploadId);// Set the response callbackcosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =(COSXMLUploadTask.COSXMLUploadTaskResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// By default, TransferManager automatically executes multipart upload for files whose sizes are equal to or greater than 2 MB. You can use the following code to change the multipart upload threshold:TransferConfig transferConfig = new TransferConfig.Builder().setDivisionForUpload(2 * 1024 * 1024) // Set multipart upload for files whose sizes are equal to or greater than 2 MB.build();TransferManager transferManager = new TransferManager(cosXmlService,transferConfig);
// If the final Complete Multipart Upload request has been initiated, the suspension will fail.boolean pauseSuccess = cosxmlUploadTask.pauseSafely();
// A suspended upload can be resumed.if (pauseSuccess) {cosxmlUploadTask.resume();}
cosxmlUploadTask.cancel();
// Absolute paths to the local filesFile[] files = new File(context.getCacheDir(), "exampleDirectory").listFiles();// Initiate a batch uploadfor (File file : files) {// If there is an uploadId for the initialized multipart upload, assign the value of uploadId here to resume the upload. Otherwise, assign nullString uploadId = null;// Upload the fileCOSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,file.getAbsolutePath(), uploadId);// Set the response callbackcosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =(COSXMLUploadTask.COSXMLUploadTaskResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});}
// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";// The location identifier of a directory in a bucket (i.e., the object key), which must end with a slash (/).String cosPath = "exampleobject/";PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, new byte[0]);cosXmlService.putObjectAsync(putObjectRequest, new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {PutObjectResult putObjectResult =(PutObjectResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";// The location identifier of a directory in a bucket (i.e., the object key), which must end with a slash (/).String cosPath = "exampleobject/";String srcPath = new File(context.getCacheDir(), "exampleobject").toString(); // Absolute path of the local filePutObjectRequest putObjectRequest= new PutObjectRequest(bucket, cosPath, srcPath);putObjectRequest.setPriorityLow(); // Set the priority of the task to low.final COSXMLUploadTask uploadTask = transferManager.upload(putObjectRequest, null);uploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {}@Overridepublic void onFail(CosXmlRequest request, CosXmlClientException clientException, CosXmlServiceException serviceException) {}});
/
; otherwise, it will be identified as a folder.APPID
) can have up to 1,000 bucket ACLs and an unlimited number of object ACLs. Do not configure ACLs for an object during upload if you don’t need to control access to it. The object will inherit the permissions of its bucket by default.// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object keyString srcPath = new File(context.getCacheDir(), "exampleobject").toString(); // The absolute path of the local filePutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath,srcPath);putObjectRequest.setProgressListener(new CosXmlProgressListener() {@Overridepublic void onProgress(long progress, long max) {// todo Do something to update progress...}});cosXmlService.putObjectAsync(putObjectRequest, new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {PutObjectResult putObjectResult = (PutObjectResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
String bucket = "examplebucket-1250000000"; // Bucket, formatted as BucketName-APPIDString cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object keyString srcPath = new File(context.getCacheDir(), "exampleobject").toString(); // The absolute path of the local filePostObjectRequest postObjectRequest = new PostObjectRequest(bucket, cosPath,srcPath);postObjectRequest.setProgressListener(new CosXmlProgressListener() {@Overridepublic void onProgress(long progress, long max) {// todo Do something to update progress...}});cosXmlService.postObjectAsync(postObjectRequest, new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {PutObjectResult putObjectResult = (PutObjectResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
Initiate Multipart Upload
and get the UploadId
.UploadId
to upload parts with Upload Part
or copy parts with Upload Part Copy
Complete Multipart Upload
.UploadId
of the multipart upload, you can query the multipart upload job with List Multipart Uploads
to get the UploadId
of the corresponding file.UploadId
to list the uploaded parts with List Parts
.UploadId
to upload the remaining parts with Upload Part
or copy the remaining parts with Upload Part Copy
.Complete Multipart Upload
.UploadId
of the multipart upload, you can query the multipart upload job with List Multipart Uploads
to get the UploadId
of the corresponding file.Abort Multipart Upload
.List Multipart Uploads
) is used to query in-progress multipart uploads in a specified bucket.// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";ListMultiUploadsRequest listMultiUploadsRequest =new ListMultiUploadsRequest(bucket);cosXmlService.listMultiUploadsAsync(listMultiUploadsRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {ListMultiUploadsResult listMultiUploadsResult =(ListMultiUploadsResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
uploadId
.// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object keyInitMultipartUploadRequest initMultipartUploadRequest =new InitMultipartUploadRequest(bucket, cosPath);cosXmlService.initMultipartUploadAsync(initMultipartUploadRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {// UploadId of the multipart uploaduploadId = ((InitMultipartUploadResult) result).initMultipartUpload.uploadId;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
Upload Part
) is used to upload an object in parts.// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object keyUploadPartRequest uploadPartRequest = new UploadPartRequest(bucket, cosPath,partNumber, srcFile.getPath(), offset, PART_SIZE, uploadId);uploadPartRequest.setProgressListener(new CosXmlProgressListener() {@Overridepublic void onProgress(long progress, long max) {// todo Do something to update progress...}});cosXmlService.uploadPartAsync(uploadPartRequest, new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {String eTag = ((UploadPartResult) result).eTag;eTags.put(partNumber, eTag);}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
List Parts
) is used to query the uploaded parts of a multipart upload.// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object keyListPartsRequest listPartsRequest = new ListPartsRequest(bucket, cosPath,uploadId);cosXmlService.listPartsAsync(listPartsRequest, new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {ListParts listParts = ((ListPartsResult) result).listParts;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
Complete Multipart Upload
) is used to complete the multipart upload of a file.// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object keyCompleteMultiUploadRequest completeMultiUploadRequest =new CompleteMultiUploadRequest(bucket,cosPath, uploadId, eTags);cosXmlService.completeMultiUploadAsync(completeMultiUploadRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {CompleteMultiUploadResult completeMultiUploadResult =(CompleteMultiUploadResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
Abort Multipart Upload
) is used to abort a multipart upload and delete the uploaded parts.// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object keyAbortMultiUploadRequest abortMultiUploadRequest =new AbortMultiUploadRequest(bucket,cosPath, uploadId);cosXmlService.abortMultiUploadAsync(abortMultiUploadRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {AbortMultiUploadResult abortMultiUploadResult =(AbortMultiUploadResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
Was this page helpful?