Uploading and Copying Objects

Last updated: 2020-09-07 16:16:13

    Overview

    This document provides an overview of APIs and SDK sample codes related to uploading and replicating objects.

    Simple operations

    API Operation Description
    PUT Object Uploading an object using simple upload Uploads an object to a bucket
    POST Object Uploading an object using HTML form Uploads an object using HTML form
    PUT Object - Copy Copying an object (modifying object attributes) Copies a file to a destination path

    Multipart operations

    API Operation Description
    List Multipart Uploads Querying multipart uploads Queries in-progress multipart uploads
    Initiate Multipart Upload Initializing a multipart upload Initializes a multipart upload
    Upload Part Uploading a part Uploads a part in multipart upload
    Upload Part - Copy Copying a part Copies an object as a part
    List Parts Querying uploaded parts Queries the uploaded parts of a specific multipart upload
    Complete Multipart Upload Completing a multipart upload Completes the multipart upload of an entire file
    Abort Multipart Upload Aborting a multipart upload Aborts a multipart upload and deletes the uploaded parts

    SDK API Reference

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

    Advanced APIs (recommended)

    Uploading an object

    The advanced upload API encapsulates both PUT Object (simple upload) and multipart upload APIs, and can automatically select the upload method based on file size. It also supports checkpoint restart for resuming a suspended multipart upload.

    Sample 1. Uploading a local file

    // Initialize `TransferConfig`. The default configuration is used here. To customize it, please see the SDK API documentation.
    TransferConfig transferConfig = new TransferConfig.Builder().build();
    // Initialize TransferManager
    TransferManager transferManager = new TransferManager(cosXmlService,
            transferConfig);
    
    String bucket = "examplebucket-1250000000"; // Bucket in the format: `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
    String uploadId = null; // If there is an uploadId for the initialized multipart upload, assign the value of the 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 upload progress callback
    cosxmlUploadTask.setCosXmlProgressListener(new CosXmlProgressListener() {
        @Override
        public void onProgress(long complete, long target) {
            // todo Do something to update progress...
        }
    });
    // Set the result callback
    cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest request, CosXmlResult result) {
            COSXMLUploadTask.COSXMLUploadTaskResult cOSXMLUploadTaskResult =
                    (COSXMLUploadTask.COSXMLUploadTaskResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest request,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });
    // Set the status callback where you can view the upload progress
    cosxmlUploadTask.setTransferStateListener(new TransferStateListener() {
        @Override
        public void onStateChanged(TransferState state) {
            // todo notify transfer state
        }
    });

    Note:

    • For the complete sample, please go to GitHub.
    • You can generate a download URL for the uploaded file using the same key. For detailed directions, see Generating a Pre-Signed Link. 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);
    
    String bucket = "examplebucket-1250000000"; // Bucket in the format: `BucketName-APPID`
    String cosPath = "exampleobject"; // The 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 result callback
    cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest request, CosXmlResult result) {
            COSXMLUploadTask.COSXMLUploadTaskResult cOSXMLUploadTaskResult =
                    (COSXMLUploadTask.COSXMLUploadTaskResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest request,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    • For the complete sample, please go to GitHub.
    • You can generate a download URL for the uploaded file using the same key. For detailed directions, see Generating a Pre-Signed Link. 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);
    
    String bucket = "examplebucket-1250000000"; // Bucket in the format: `BucketName-APPID`
    String cosPath = "exampleobject"; // The 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 result callback 
    cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest request, CosXmlResult result) {
            COSXMLUploadTask.COSXMLUploadTaskResult cOSXMLUploadTaskResult =
                    (COSXMLUploadTask.COSXMLUploadTaskResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest request,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    • For the complete sample, please go to GitHub.
    • You can generate a download URL for the uploaded file using the same key. For detailed directions, see Generating a Pre-Signed Link. 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);
    
    String bucket = "examplebucket-1250000000"; // Bucket in the format: `BucketName-APPID`
    String cosPath = "exampleobject"; // The 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 result callback
    cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest request, CosXmlResult result) {
            COSXMLUploadTask.COSXMLUploadTaskResult cOSXMLUploadTaskResult =
                    (COSXMLUploadTask.COSXMLUploadTaskResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest request,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    • For the complete sample, please go to GitHub.
    • You can generate a download URL for the uploaded file using the same key. For detailed directions, see Generating a Pre-Signed Link. Please note that for private-read files, the download URL is only valid for a limited period of time.

    Sample 5. 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 suspend operation will fail
    boolean pauseSuccess = cosxmlUploadTask.pauseSafely();

    To resume a suspended download, run the code below:

    // A suspended upload can be resumed
    if (pauseSuccess) {
        cosxmlUploadTask.resume();
    }

    To cancel an upload, run this code:

    cosxmlUploadTask.cancel();

    Note:

    • For the complete sample, please go to GitHub.

    Sample 6. Uploading multiple objects

    // Absolute paths to the local files
    File[] files = new File(context.getCacheDir(), "exampleDirectory").listFiles();
    
    // Initiate the upload
    for (File file : files) {
        String uploadId = null; // If there is an uploadId for the initialized multipart upload, assign the value of the uploadId here to resume the upload; otherwise, assign null
        String uploadId = null;
    
        // Upload the files
        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 cOSXMLUploadTaskResult =
                        (COSXMLUploadTask.COSXMLUploadTaskResult) result;
            }
    
            @Override
            public void onFail(CosXmlRequest request,
                               CosXmlClientException clientException,
                               CosXmlServiceException serviceException) {
                if (clientException != null) {
                    clientException.printStackTrace();
                } else {
                    serviceException.printStackTrace();
                }
            }
        });
    }

    Sample 7. Creating a directory

    String bucket = "examplebucket-1250000000"; // Bucket name in the format: `BucketName-APPID`
    // The location identifier of a folder in a bucket, i.e. object key, which 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;
        }
    
        @Override
        public void onFail(CosXmlRequest request,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    • For the complete sample, please go to GitHub.
    • You can generate a download URL for the uploaded file using the same key. For detailed directions, see Generating a Pre-Signed Link. Please note that for private-read files, the download URL is only valid for a limited period of time.

    Copying an object

    The advanced replication API encapsulates PUT Object - Copy, and the APIs for multipart replication to allow asynchronous requests for suspending, resuming and canceling a replication request.

    Sample code

    // Initialize `TransferConfig`. The default configuration is used here. If you need to customize the configuration, please see the SDK API documentation.
    TransferConfig transferConfig = new TransferConfig.Builder().build();
    // Initialize TransferManager
    TransferManager transferManager = new TransferManager(cosXmlService,
            transferConfig);
    
    String sourceAppid = "1250000000"; // Account APPID
    String sourceBucket = "sourcebucket-1250000000"; // Bucket of the source object
    String sourceRegion = "COS_REGION"; // Region where the bucket of the source object resides
    String sourceCosPath = "sourceObject"; // Key of the source object
    // Construct the source object attributes
    CopyObjectRequest.CopySourceStruct copySourceStruct =
            new CopyObjectRequest.CopySourceStruct(
                    sourceAppid, sourceBucket, sourceRegion, sourceCosPath);
    // Destination bucket
    String bucket = "examplebucket-1250000000"; // Bucket name in the format: `BucketName-APPID`
    // Destination object
    String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e. the object key
    
    // Copy the object
    COSXMLCopyTask cosxmlCopyTask = transferManager.copy(bucket, cosPath,
            copySourceStruct);
    
    // Set result callback
    cosxmlCopyTask.setCosXmlResultListener(new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest request, CosXmlResult result) {
            COSXMLCopyTask.COSXMLCopyTaskResult cOSXMLCopyTaskResult =
                    (COSXMLCopyTask.COSXMLCopyTaskResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest request,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });
    // Set the status callback where you can view the replication progress
    cosxmlCopyTask.setTransferStateListener(new TransferStateListener() {
        @Override
        public void onStateChanged(TransferState state) {
            // todo notify transfer state
        }
    });

    Note:

    For the complete sample, please go to GitHub.

    Simple Operations

    Uploading an object using simple upload

    API description

    This API is used to upload an object to a specified bucket. This operation requires the requester to have WRITE permission for the bucket and can upload a file of up to 5 GB in size. To upload larger objects, please use Multipart Upload or the advanced upload API.

    Note:

    1. The Key (file name) cannot end with /; otherwise, it will be identified as a folder.
    2. Each root account (APPID) can have up to 1,000 bucket ACLs and an unlimited number of object ACLs. If you do not need ACL control over an object, please leave it to the default option of inheriting bucket permissions.

    Sample code

    String bucket = "examplebucket-1250000000"; // Bucket name in the format: `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
    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;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    • For the complete sample, please go to GitHub.
    • You can generate a download URL for the uploaded file using the same key. For detailed directions, see Generating a Pre-Signed Link. Please note that for private-read files, the download URL is only valid for a limited period of time.

    Uploading an object using HTML form

    API description

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

    Sample code

    String bucket = "examplebucket-1250000000"; // Bucket name in the format: 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();//"Absolute path of 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;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    For the complete sample, please go to GitHub.

    Copying an object (modifying object attributes)

    This API is used to copy a file to a destination path.

    Sample 1. Copying an object while retaining its attributes

    String sourceAppid = "1250000000"; // Account APPID
    String sourceBucket = "sourcebucket-1250000000"; // Bucket of the source object
    String sourceRegion = "COS_REGION"; // Region where the bucket of the source object resides
    String sourceCosPath = "sourceObject"; // Key of the source object
    // Construct the source object attributes
    CopyObjectRequest.CopySourceStruct copySourceStruct =
            new CopyObjectRequest.CopySourceStruct(
            sourceAppid, sourceBucket, sourceRegion, sourceCosPath);
    
    String bucket = "examplebucket-1250000000"; // Bucket name in the format: `BucketName-APPID`
    String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e. the object key
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, cosPath,
            copySourceStruct);
    
    cosXmlService.copyObjectAsync(copyObjectRequest, new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest request, CosXmlResult result) {
            CopyObjectResult copyObjectResult = (CopyObjectResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    For the complete sample, please go to GitHub.

    Sample 2. Copying an object while replacing its attributes

    String sourceAppid = "1250000000"; // Account APPID
    String sourceBucket = "sourcebucket-1250000000"; // Bucket of the source object
    String sourceRegion = "COS_REGION"; // Region where the bucket of the source object resides
    String sourceCosPath = "sourceObject"; // Key of the source object
    // Construct the source object attributes
    CopyObjectRequest.CopySourceStruct copySourceStruct =
            new CopyObjectRequest.CopySourceStruct(
            sourceAppid, sourceBucket, sourceRegion, sourceCosPath);
    
    String bucket = "examplebucket-1250000000"; // Bucket name in the format: `BucketName-APPID`
    String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e. the object key
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, cosPath,
            copySourceStruct);
    copyObjectRequest.setCopyMetaDataDirective(MetaDataDirective.REPLACED);
    copyObjectRequest.setXCOSMeta("x-cos-metadata-oldKey", "newValue");
    
    cosXmlService.copyObjectAsync(copyObjectRequest, new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest request, CosXmlResult result) {
            CopyObjectResult copyObjectResult = (CopyObjectResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    For the complete sample, please go to GitHub.

    Sample 3. Modifying object metadata

    String appId = "1250000000"; // Account APPID
    String bucket = "examplebucket-1250000000"; // Bucket name in the format: `BucketName-APPID`
    String region = "COS_REGION"; // Region where the bucket of the source object resides
    String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e. the object key
    // Construct the source object attributes
    CopyObjectRequest.CopySourceStruct copySourceStruct =
            new CopyObjectRequest.CopySourceStruct(
            appId, bucket, region, cosPath);
    
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, cosPath,
            copySourceStruct);
    copyObjectRequest.setCopyMetaDataDirective(MetaDataDirective.REPLACED);
    // Replace metadata
    copyObjectRequest.setXCOSMeta("x-cos-metadata-oldKey", "newValue");
    
    cosXmlService.copyObjectAsync(copyObjectRequest, new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest request, CosXmlResult result) {
            CopyObjectResult copyObjectResult = (CopyObjectResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    For the complete sample, please go to GitHub.

    Sample 4. Modifying the storage class of an object

    String appId = "1250000000"; // Account APPID
    String bucket = "examplebucket-1250000000"; // Bucket name in the format: `BucketName-APPID`
    String region = "COS_REGION"; // Region where the bucket of the source object resides
    String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e. the object key
    // Construct the source object attributes
    CopyObjectRequest.CopySourceStruct copySourceStruct =
            new CopyObjectRequest.CopySourceStruct(
            appId, bucket, region, cosPath);
    
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, cosPath,
            copySourceStruct);
    // Set the storage class to STANDARD_IA
    copyObjectRequest.setCosStorageClass(COSStorageClass.STANDARD_IA);
    
    cosXmlService.copyObjectAsync(copyObjectRequest, new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest request, CosXmlResult result) {
            CopyObjectResult copyObjectResult = (CopyObjectResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    For the complete sample, please go to GitHub.

    Multipart Operations

    The multipart operation process is outlined below.

    How to perform a multipart upload/replication

    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.

    How to resume a multipart upload/replication

    1. If you did not record the UploadId of the multipart upload, you can query the multipart upload with List Multipart Uploads to get it.
    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.

    How to abort a multipart upload/replication

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

    Querying multipart uploads

    API description

    This API is used to query the in-progress multipart uploads operations in a specified bucket.

    Sample code

    String bucket = "examplebucket-1250000000"; // Format: BucketName-APPID
    ListMultiUploadsRequest listMultiUploadsRequest =
            new ListMultiUploadsRequest(bucket);
    cosXmlService.listMultiUploadsAsync(listMultiUploadsRequest,
            new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {
            ListMultiUploadsResult listMultiUploadsResult =
                    (ListMultiUploadsResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    For the complete sample, please go to GitHub.

    Initializing a multipart upload

    API description

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

    Sample code

    String bucket = "examplebucket-1250000000"; // Format: BucketName-APPID
    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;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    For the complete sample, please go to GitHub.

    Uploading a part

    This API is used to upload parts in a multipart upload operation.

    Sample code

    String bucket = "examplebucket-1250000000"; // Bucket name in the format: `BucketName-APPID`
    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);
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    For the complete sample, please go to GitHub.

    Copying a part

    API description

    This API is used to copy an object as a part.

    Sample code

    String sourceAppid = "1250000000"; // Account APPID
    String sourceBucket = "sourcebucket-1250000000"; // Bucket of the source object
    String sourceRegion = "COS_REGION"; // Region where the bucket of the source object resides
    String sourceCosPath = "sourceObject"; // Key of the source object
    // Construct the source object attributes
    CopyObjectRequest.CopySourceStruct copySourceStruct =
            new CopyObjectRequest.CopySourceStruct(
            sourceAppid, sourceBucket, sourceRegion, sourceCosPath);
    
    String bucket = "examplebucket-1250000000"; // Bucket name in the format: `BucketName-APPID`
    String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e. the object key
    
    String uploadId = "exampleUploadId";
    int partNumber = 1; // Part number
    long start = 0; // The starting part of the source object to be copied
    long end = 1023; //The ending part of the source object to be copied
    
    UploadPartCopyRequest uploadPartCopyRequest =
            new UploadPartCopyRequest(bucket, cosPath,
            partNumber, uploadId, copySourceStruct, start, end);
    cosXmlService.copyObjectAsync(uploadPartCopyRequest,
            new CosXmlResultListener() {
        @Override
        public void onSuccess(CosXmlRequest request, CosXmlResult result) {
            UploadPartCopyResult uploadPartCopyResult =
                    (UploadPartCopyResult) result;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    For the complete sample, please go to GitHub.

    Querying uploaded parts

    API description

    This API is used to query the uploaded parts of a specific multipart upload.

    Sample code

    String bucket = "examplebucket-1250000000"; // Format: BucketName-APPID
    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;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    For the complete sample, please go to GitHub.

    Completing a multipart upload

    API description

    This API is used to complete the multipart upload of an entire file.

    Sample code

    String bucket = "examplebucket-1250000000"; // Format: BucketName-APPID
    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;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    For the complete sample, please go to GitHub.

    Aborting a multipart upload

    API description

    This API is used to abort a multipart upload and delete the uploaded parts.

    Sample code

    String bucket = "examplebucket-1250000000"; // Format: BucketName-APPID
    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;
        }
    
        @Override
        public void onFail(CosXmlRequest cosXmlRequest,
                           CosXmlClientException clientException,
                           CosXmlServiceException serviceException) {
            if (clientException != null) {
                clientException.printStackTrace();
            } else {
                serviceException.printStackTrace();
            }
        }
    });

    Note:

    For the complete sample, please go to GitHub.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help