cos:PutObject. For more authorization details, see CAM-Supported Service APIs.cos:InitiateMultipartUpload, cos:UploadPart, cos:CompleteMultipartUpload. For more authorization details, see CAM-Supported Service APIs.cos:InitiateMultipartUpload, cos:UploadPart, cos:CompleteMultipartUpload, cos:ListMultipartUploads, cos:ListParts. For more authorization details, see CAM-Supported Service APIs.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). |
// 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 codeTransferConfig 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 TransferManagerTransferManager transferManager = new TransferManager(cosXmlService,transferConfig);
// 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/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object in the bucketString 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 nullString 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() {@Overridepublic void onSuccess(InitiateMultipartUpload initiateMultipartUpload) {// uploadId used for resuming the upload next timeString 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 return result callbackcosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic 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?@Overridepublic 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 progresscosxmlUploadTask.setTransferStateListener(new TransferStateListener() {@Overridepublic void onStateChanged(TransferState state) {// todo notify transfer state}});
// 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/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object in the bucket// Upload byte arraybyte[] bytes = "this is a test".getBytes(Charset.forName("UTF-8"));COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,bytes);// Set the return result callbackcosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// 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/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object in the bucket// Streaming uploadInputStream inputStream =new ByteArrayInputStream("this is a test".getBytes(Charset.forName("UTF-8")));COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,inputStream);// Set the return result callbackcosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// 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/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object in the bucket// The file's UriUri uri = Uri.parse("exampleObject");// If an UploadId for initializing multipart upload exists, assign the corresponding uploadId value to resume the upload; otherwise, assign nullString uploadId = null;COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,uri, uploadId);// Set the return result callbackcosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// If the final Complete Multipart Upload request has been initiated during the upload process, pausing will failboolean pauseSuccess = cosxmlUploadTask.pauseSafely();
// If paused successfully, the upload can be resumedif (pauseSuccess) {cosxmlUploadTask.resume();}
cosxmlUploadTask.cancel();
// Absolute path of the local fileFile[] files = new File(context.getCacheDir(), "exampleDirectory").listFiles();// Start batch uploadfor (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 callbackcosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});}
// 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/bucketString 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() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// 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/bucketString 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 filePutObjectRequest putObjectRequest= new PutObjectRequest(bucket, cosPath, srcPath);putObjectRequest.setPriorityLow(); // This is set as a low priority upload taskfinal 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) {}});
// 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/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject.txt"; // the object key, which identifies the location of the object in the bucketString 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 nullString 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 callbackcosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
/, otherwise it will be recognized as a folder.// 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/bucketString 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 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 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// 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/bucketString 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() {@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 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// 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/bucketString 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() {@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 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// 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/bucketString 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 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// 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/bucketString 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() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// 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/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject.txt"; // 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 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// 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/bucketString 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() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// 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/bucketString 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() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// 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/bucketString 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() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
Feedback