This topic describes how to copy an object within a bucket or across buckets in the same region.

Usage notes

  • To copy an object, you must have read permissions on the source object and read and write permissions on the destination bucket.
  • The source bucket and destination bucket must be in the same region. For example, objects in a bucket located in the China (Hangzhou) region cannot be copied to another bucket located in the China (Qingdao) region.
  • The size of the object that you want to copy cannot exceed 1 GB.

Sample code

The following code provides an example on how to copy an object:

// Specify the name of the source bucket. 
String srcBucketName = "srcbucket";
// Specify the full path of the source object. 
String srcObjectKey = "dir1/srcobject.txt";
// Specify the name of the destination bucket. The destination bucket must be in the same region as the source bucket. 
String destBucketName = "destbucket";
// Specify the full path of the destination object. 
String destObjectKey = "dir2/destobject.txt";
// Construct a request to copy the object. 
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketName, srcObjectKey, destBucketName, destObjectKey);

// ObjectMetadata objectMetadata = new ObjectMetadata();
// Set the access control list (ACL) of the object. In this example, the ACL is set to private. 
// objectMetadata.setHeader("x-oss-object-acl", "private");
// Specify the storage class of the object. In this example, the storage class is set to Standard. 
// objectMetadata.setHeader("x-oss-storage-class", "Standard");
// Specify whether the CopyObject operation overwrites an object with the same name. In this example, this parameter is set to true, which indicates that the object with the same name cannot be overwritten. 
// objectMetadata.setHeader("x-oss-forbid-overwrite", "true");
// If the ETag value of the source object is the same as the ETag value that is specified in the request, OSS copies the object. 
// objectMetadata.setHeader("x-oss-copy-source-if-match", "5B3C1A2E053D763E1B002CC607C5****");
// Specify the path of the source object. 
// objectMetadata.setHeader("x-oss-copy-source", "/examplebucket/recode-test.txt");
// If the ETag value of the source object is different from the ETag value that is specified in the request, OSS copies the object. 
// objectMetadata.setHeader("x-oss-copy-source-if-none-match", "5B3C1A2E053D763E1B002CC607C5****");
// If the time that is specified in the request is equal to or later than the time when the object is modified, OSS copies the object. 
// objectMetadata.setHeader("x-oss-copy-source-if-unmodified-since", "2021-12-09T07:01:56.000Z");
// If the source object is modified after the time that is specified in the request, OSS copies the object. 
// objectMetadata.setHeader("x-oss-copy-source-if-modified-since", "2021-12-09T07:01:56.000Z");
// Specify the method that is used to configure the metadata of the destination object. In this example, the method is set to COPY, which indicates that the metadata of the source object is copied to the destination object. 
// objectMetadata.setHeader("x-oss-metadata-directive", "COPY");
// Specify the server-side encryption algorithm that is used to encrypt the destination object. 
// objectMetadata.setHeader("x-oss-server-side-encryption", "SSE-KMS");
// Specify the customer master key (CMK) that is managed by Key Management Service (KMS). This parameter takes effect only when you set x-oss-server-side-encryption to KMS. 
// objectMetadata.setHeader("x-oss-server-side-encryption-key-id", "9468da86-3509-4f8d-a61e-6eab1eac****");
// Specify tags for the object. You can specify multiple tags at a time. 
// objectMetadata.setHeader("x-oss-tagging", "a:1");
// Specify the method that is used to configure tags for the destination object. In this example, the method is set to COPY, which indicates that the tags of the source object are copied to the destination object. 
// objectMetadata.setHeader("x-oss-tagging-directive", "COPY");

// Copy the object in asynchronous mode. 
OSSAsyncTask copyTask = oss.asyncCopyObject(copyObjectRequest, new OSSCompletedCallback<CopyObjectRequest, CopyObjectResult>() {
    @Override
    public void onSuccess(CopyObjectRequest request, CopyObjectResult result) {
        Log.d("copyObject", "copy success!");
    }

    @Override
    public void onFailure(CopyObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
        // Handle request exceptions. 
        if (clientExcepion != null) {
            // Handle client-side exceptions, such as network errors. 
            clientExcepion.printStackTrace();
        }
        if (serviceException != null) {
            // Handle server-side exceptions. 
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

References

  • For more information about the complete sample code that is used to copy an object, visit GitHub.
  • For more information about the API operation that you can call to copy an object, see CopyObject.