tencent cloud

Feedback

Uploading an Object

Last updated: 2024-06-25 10:53:13

    Overview

    This document provides an overview of APIs and SDK sample codes related to uploading and replicating objects.
    Simple operations
    API
    Operation
    Description
    Uploading an object
    Uploads an object to a bucket.
    Multipart operations
    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.

    SDK API References

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

    Advanced APIs (Recommended)

    Uploading an object

    The advanced APIs encapsulate the simple upload and multipart upload APIs and can intelligently select the upload method based on file size. They also support checkpoint restart for resuming interrupted operations.

    Sample 1. Uploading a local file

    // 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 TransferManager
    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
    String 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 null
    String uploadId = null;
    
    // Upload the file
    COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,
    srcPath, uploadId);
    
    // Set the callback for initializing multipart upload (supported starting from v5.9.7)
    cosxmlUploadTask.setInitMultipleUploadListener(new InitMultipleUploadListener() {
    @Override
    public void onSuccess(InitiateMultipartUpload initiateMultipartUpload) {
    // The `uploadId` used for next upload
    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 response callback
    cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
    @Override
    public 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?
    @Override
    public 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 progress
    cosxmlUploadTask.setTransferStateListener(new TransferStateListener() {
    @Override
    public void onStateChanged(TransferState state) {
    // todo notify transfer state
    }
    });
    Note:
    For the complete sample, please visit GitHub.
    You can generate a download URL for the uploaded file using the same key. For detailed directions, see Generating Pre-Signed URLs. Please note that for private-read files, the download URL is only valid for a limited period of time.

    Sample 2. Uploading binary data

    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 array
    byte[] bytes = "this is a test".getBytes(Charset.forName("UTF-8"));
    COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,
    bytes);
    
    // Set the response callback
    cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
    @Override
    public 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?
    @Override
    public void onFail(CosXmlRequest request,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    Note:
    For the complete sample, please visit GitHub.
    You can generate a download URL for the uploaded file using the same key. For detailed directions, see Generating Pre-Signed URLs. Please note that for private-read files, the download URL is only valid for a limited period of time.

    Sample 3. Stream upload

    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 upload
    InputStream inputStream =
    new ByteArrayInputStream("this is a test".getBytes(Charset.forName(
    "UTF-8")));
    COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,
    inputStream);
    
    // Set the response callback
    cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
    @Override
    public 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?
    @Override
    public void onFail(CosXmlRequest request,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    Note:
    For the complete sample, please visit GitHub.
    You can generate a download URL for the uploaded file using the same key. For detailed directions, see Generating Pre-Signed URLs. Please note that for private-read files, the download URL is only valid for a limited period of time.

    Sample 4. Uploading using a URI

    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 file
    Uri 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 callback
    cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
    @Override
    public 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?
    @Override
    public void onFail(CosXmlRequest request,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    Note:
    For the complete sample, please visit GitHub.
    You can generate a download URL for the uploaded file using the same key. For detailed directions, see Generating Pre-Signed URLs. Please note that for private-read files, the download URL is only valid for a limited period of time.

    Sample 5. Setting the threshold for smart multipart upload

    // 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);
    Note:
    For the complete sample, please visit GitHub.

    Sample 6. Suspending, resuming, and canceling an upload

    To suspend an upload, use the code below:
    // If the final Complete Multipart Upload request has been initiated, the suspension will fail.
    boolean pauseSuccess = cosxmlUploadTask.pauseSafely();
    To resume a suspended download, use the code below:
    // A suspended upload can be resumed.
    if (pauseSuccess) {
    cosxmlUploadTask.resume();
    }
    To cancel an upload, use the code below:
    cosxmlUploadTask.cancel();
    Note:
    For the complete sample, please visit GitHub.

    Sample 7. Uploading multiple objects

    // Absolute paths to the local files
    File[] files = new File(context.getCacheDir(), "exampleDirectory").listFiles();
    
    // Initiate a batch upload
    for (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 null
    String uploadId = null;
    
    // Upload the file
    COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,
    file.getAbsolutePath(), uploadId);
    
    // Set the response callback
    cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
    @Override
    public 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?
    @Override
    public void onFail(CosXmlRequest request,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    }
    Note:
    For the complete sample, please visit GitHub.

    Sample 8. Creating a directory

    // 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() {
    @Override
    public 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?
    @Override
    public void onFail(CosXmlRequest request,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    Note:
    For the complete sample, please visit GitHub.
    You can generate a download URL for the uploaded file using the same key. For detailed directions, see Generating Pre-Signed URLs. Please note that for private-read files, the download URL is only valid for a limited period of time.

    Sample 9. Setting a low-priority task

    // 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 file
    PutObjectRequest 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() {
    @Override
    public void onSuccess(CosXmlRequest request, CosXmlResult result) {}
    
    
    @Override
    public void onFail(CosXmlRequest request, CosXmlClientException clientException, CosXmlServiceException serviceException) {}
    });
    Note:
    For the complete sample, please visit GitHub.

    Simple Operations

    Uploading an object using simple upload

    Feature description

    This API (PUT Object) is used to upload an object smaller than 5 GB to a specified bucket. To call this API, you need to have permission to write to the bucket. If the object size is larger than 5 GB, please use Multipart Upload or Advanced APIs for the upload.
    Note:
    The key (filename) cannot end with /; otherwise, it will be identified as a folder.
    Each root account (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.

    Sample code

    // 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 key
    String srcPath = new File(context.getCacheDir(), "exampleobject")
    .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 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?
    @Override
    public void onFail(CosXmlRequest cosXmlRequest,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    Note:
    For more samples, please visit GitHub.
    You can generate a download URL for the uploaded file using the same key. For detailed directions, see Generating Pre-Signed URLs. Please note that for private-read files, the download URL is only valid for a limited period of time.

    Uploading an object using an HTML form

    Feature description

    This API is used to upload an object using an HTML form.

    Sample code

    String bucket = "examplebucket-1250000000"; // Bucket, formatted as BucketName-APPID
    String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object key
    String srcPath = new File(context.getCacheDir(), "exampleobject")
    .toString(); // The absolute path of the local file
    
    PostObjectRequest postObjectRequest = new PostObjectRequest(bucket, cosPath,
    srcPath);
    
    postObjectRequest.setProgressListener(new CosXmlProgressListener() {
    @Override
    public void onProgress(long progress, long max) {
    // todo Do something to update progress...
    }
    });
    cosXmlService.postObjectAsync(postObjectRequest, new CosXmlResultListener() {
    @Override
    public 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?
    @Override
    public void onFail(CosXmlRequest cosXmlRequest,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    Note:
    For more samples, please visit GitHub.

    Multipart Operations

    The multipart upload process is outlined below.

    Performing a multipart upload

    1. Initialize the multipart upload with Initiate Multipart Upload and get the UploadId.
    2. Use the UploadId to upload parts with Upload Part or copy parts with Upload Part Copy
    3. Complete the multipart upload with Complete Multipart Upload.

    Resuming a multipart upload

    1. If you did not record the UploadId of the multipart upload, you can query the multipart upload job with List Multipart Uploads to get the UploadId of the corresponding file.
    2. Use the UploadId to list the uploaded parts with List Parts.
    3. Use the UploadId to upload the remaining parts with Upload Part or copy the remaining parts with Upload Part Copy.
    4. Complete the multipart upload with Complete Multipart Upload.

    Aborting a multipart upload

    1. If you did not record the UploadId of the multipart upload, you can query the multipart upload job with List Multipart Uploads to get the UploadId of the corresponding file.
    2. Abort the multipart upload and delete the uploaded parts with Abort Multipart Upload.

    Querying multipart uploads

    Feature description

    This API (List Multipart Uploads) is used to query in-progress multipart uploads in a specified bucket.

    Sample code

    // 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() {
    @Override
    public 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?
    @Override
    public void onFail(CosXmlRequest cosXmlRequest,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    Note:
    For more samples, please visit GitHub.

    Initializing a multipart upload

    Feature description

    This API is used to initialize a multipart upload operation and get its uploadId.

    Sample code

    // 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 key
    
    InitMultipartUploadRequest initMultipartUploadRequest =
    new InitMultipartUploadRequest(bucket, cosPath);
    cosXmlService.initMultipartUploadAsync(initMultipartUploadRequest,
    new CosXmlResultListener() {
    @Override
    public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
    // UploadId of the multipart upload
    uploadId = ((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?
    @Override
    public void onFail(CosXmlRequest cosXmlRequest,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    Note:
    For more samples, please visit GitHub.

    Uploading parts

    This API (Upload Part) is used to upload an object in parts.

    Sample code

    // 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 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 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?
    @Override
    public void onFail(CosXmlRequest cosXmlRequest,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    Note:
    For more samples, please visit GitHub.

    Querying uploaded parts

    Feature description

    This API (List Parts) is used to query the uploaded parts of a multipart upload.

    Sample code

    // 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 key
    
    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 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?
    @Override
    public void onFail(CosXmlRequest cosXmlRequest,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    Note:
    For more samples, please visit GitHub.

    Completing a multipart upload

    Feature description

    This API (Complete Multipart Upload) is used to complete the multipart upload of a file.

    Sample code

    // 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 key
    
    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 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?
    @Override
    public void onFail(CosXmlRequest cosXmlRequest,
    @Nullable CosXmlClientException clientException,
    @Nullable CosXmlServiceException serviceException) {
    if (clientException != null) {
    clientException.printStackTrace();
    } else {
    serviceException.printStackTrace();
    }
    }
    });
    Note:
    For more samples, please visit GitHub.

    Aborting a multipart upload

    Feature description

    This API (Abort Multipart Upload) is used to abort a multipart upload and delete the uploaded parts.

    Sample code

    // 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 key
    
    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 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?
    @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, visit GitHub.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support