tencent cloud

Cloud Object Storage

Copying and Moving Objects

Download
Modo Foco
Tamanho da Fonte
Última atualização: 2026-05-15 15:02:35

Introduction

This document provides example code and descriptions for copying and moving objects in COS using the Android SDK. The content is divided into two parts: the advanced API and the simple operation.

Must-Knows

If you use the simple API to copy an object, you must have read permission for the source object and write permission for the target object. When you configure an authorization policy, set the action for the target object to cos:PutObject and the action for the source object to cos:GetObject. For more authorization information, see CAM-supported business APIs.
If you use the advanced API's automatic multipart copy or the simple API's multipart operation to copy an object in parts, you must have read permission for the source object and permissions to initiate a multipart upload, upload parts, and complete a multipart upload for the target object. When you configure an authorization policy, the action for the target object must be set to cos:InitiateMultipartUpload, cos:PutObject, and cos:CompleteMultipartUpload, and the action for the source object must be set to cos:GetObject. For more authorization information, see CAM-supported business APIs.
To move an object, you must have the permission to delete the source object in addition to the copy-related permissions mentioned above. When you configure an authorization policy, set the action for the source object to cos:DeleteObject. For more authorization information, see CAM-supported business APIs.

Related Examples

Feature Name
Description
Example code
Advanced API
The advanced interface encapsulates the simple copy and multipart copy APIs, and intelligently selects the method for copying objects based on file size.
Simple Operations
The simple copy API can copy an object to a specified bucket and does not support automatic multipart copy. It supports copying objects up to 5 GB in size. For objects larger than 5 GB, use the multipart copy or advanced interface for copying.

Advanced APIs

The advanced API encapsulates the asynchronous requests of the simple copy and multipart copy APIs, and supports pausing, resuming, and canceling copy requests.

Prerequisites: Creating a TransferManager

Before using the advanced API, you must first create a TransferManager instance. Before creating a TransferManager instance, you need to create a CosXmlService instance. For detailed code, see Creating CosXmlService.
// Initialize TransferConfig. The default configuration is used here. If you need to customize it, see the SDK API documentation.
// By default, files larger than or equal to 5 MB are automatically copied in parts, with a part size of 5 MB. You can modify the part size threshold using the following code.
TransferConfig transferConfig = new TransferConfig.Builder() // Set the minimum object size to enable multipart copy. The default is 5 MB. .setDividsionForCopy(5242880) // Set the part size for multipart copy. The default is 5 MB. .setSliceSizeForCopy(5242880) .build();
// Initialize TransferManager
TransferManager transferManager = new TransferManager(cosXmlService,
transferConfig);

Use Case: Copying Objects

String sourceBucket = "sourcebucket-1250000000"; // The bucket where the source object resides
String sourceRegion = "COS_REGION"; // The region where the source object's bucket resides
String sourceCosPath = "sourceObject"; // The object key of the source object
// Construct source object properties
CopyObjectRequest.CopySourceStruct copySourceStruct =
new CopyObjectRequest.CopySourceStruct(sourceBucket, sourceRegion, sourceCosPath);
// Target bucket
// A bucket name consists of bucketname-appid. The appid must be included. You can view bucket names in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
// Target object
String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object key.

// Copy object
COSXMLCopyTask cosxmlCopyTask = transferManager.copy(bucket, cosPath,
copySourceStruct);

// Set the result callback
cosxmlCopyTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
COSXMLCopyTask.COSXMLCopyTaskResult copyResult =
(COSXMLCopyTask.COSXMLCopyTaskResult) result;
}

// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public 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 progress.
cosxmlCopyTask.setTransferStateListener(new TransferStateListener() {
@Override
public void onStateChanged(TransferState state) {
// todo notify transfer state
}
});

Use Case: Moving Objects

Moving an object primarily involves two operations: copying the source object to the target location and deleting the source object.
COS identifies objects by bucket name (Bucket) and object key (ObjectKey). Moving an object means modifying its identifier. The COS Android SDK currently does not provide a dedicated API for modifying an object's unique identifier. However, you can achieve the goal of modifying an object's identifier, and thus moving the object, by combining the basic operations of copying the object and deleting the object.
For example, to move the object named sourceObject from the bucket sourcebucket-1250000000 to the bucket examplebucket-1250000000 and rename it to exampleobject, you can first copy the sourceObject to the examplebucket-1250000000 bucket and set its object key to exampleobject. After the copy is complete, delete the original sourceObject object to achieve the "move" effect.
final String sourceBucket = "sourcebucket-1250000000"; // The bucket where the source object resides
final String sourceRegion = "COS_REGION"; // The region where the source object's bucket resides
final String sourceKey = "sourceObject"; // The key of the source object
// Construct source object properties
CopyObjectRequest.CopySourceStruct copySource = new CopyObjectRequest.CopySourceStruct(sourceBucket,
sourceRegion, sourceKey);

String bucket = "examplebucket-1250000000"; // The target bucket. Format: BucketName-APPID
String key = "exampleobject"; // The object key of the target object.

// copy(String bucket, String cosPath, CopyObjectRequest.CopySourceStruct copySourceStruct){
COSXMLCopyTask copyTask = transferManager.copy(bucket, key, copySource);
copyTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
try {
// Delete the file after the copy operation succeeds.
DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(sourceBucket, sourceKey);
DeleteObjectResult deleteResult = cosXmlService.deleteObject(deleteObjectRequest);
} catch (CosXmlClientException e) {
e.printStackTrace();
} catch (CosXmlServiceException e) {
e.printStackTrace();
}
}

@Override
public void onFail(CosXmlRequest request, CosXmlClientException exception, CosXmlServiceException serviceException) {

}
});
Note:
For more complete examples, see GitHub.

Copying an Object

Copy the file to the target path (PUT Object-Copy).

Prerequisites: Creating a CosXmlService

Before calling a COS API, you must first create a CosXmlService instance. For detailed code, see Creating CosXmlService.

Use Case: Preserving Object Attributes When Objects Are Copied

String sourceBucket = "sourcebucket-1250000000"; // The bucket where the source object resides
String sourceRegion = "COS_REGION"; // The region where the source object's bucket resides
String sourceCosPath = "sourceObject"; // The key of the source object
// Construct source object properties
CopyObjectRequest.CopySourceStruct copySourceStruct =
new CopyObjectRequest.CopySourceStruct(sourceBucket, sourceRegion, sourceCosPath);

// A bucket name consists of bucketname-appid. The appid must be included. You can view bucket names in the COS console. 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.
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, cosPath,
copySourceStruct);

cosXmlService.copyObjectAsync(copyObjectRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
CopyObjectResult copyObjectResult = (CopyObjectResult) result;
}

// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Replacing Object Attributes When Objects Are Copied

String sourceBucket = "sourcebucket-1250000000"; // The bucket where the source object resides
String sourceRegion = "COS_REGION"; // The region where the source object's bucket resides
String sourceCosPath = "sourceObject"; // The key of the source object
// Construct source object properties
CopyObjectRequest.CopySourceStruct copySourceStruct =
new CopyObjectRequest.CopySourceStruct(sourceBucket, sourceRegion, sourceCosPath);

// A bucket name consists of bucketname-appid. The appid must be included. You can view bucket names in the COS console. 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.
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;
}

// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is CosXmlServiceException?
@Override
public void onFail(CosXmlRequest cosXmlRequest,
@Nullable CosXmlClientException clientException,
@Nullable CosXmlServiceException serviceException) {
if (clientException != null) {
clientException.printStackTrace();
} else {
serviceException.printStackTrace();
}
}
});

Use Case: Modifying Object Metadata

// A bucket name consists of bucketname-appid. The appid must be included. You can view bucket names in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String region = "COS_REGION"; // The region where the source object's bucket resides
String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object key.
// Construct source object properties
CopyObjectRequest.CopySourceStruct copySourceStruct =
new CopyObjectRequest.CopySourceStruct(bucket, region, cosPath);

CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, cosPath,
copySourceStruct);
copyObjectRequest.setCopyMetaDataDirective(MetaDataDirective.REPLACED);
// Modify the metadata to the new value
copyObjectRequest.setXCOSMeta("x-cos-metadata-oldKey", "newValue");

cosXmlService.copyObjectAsync(copyObjectRequest, new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
CopyObjectResult copyObjectResult = (CopyObjectResult) result;
}

// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is 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 examples, see GitHub.

Use Case: Modifying COS Storage Classes

// A bucket name consists of bucketname-appid. The appid must be included. You can view bucket names in the COS console. https://console.tencentcloud.com/cos5/bucket
String bucket = "examplebucket-1250000000";
String region = "COS_REGION"; // The region where the source object's bucket resides
String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object key.
// Construct source object properties
CopyObjectRequest.CopySourceStruct copySourceStruct =
new CopyObjectRequest.CopySourceStruct(bucket, region, cosPath);

CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, cosPath,
copySourceStruct);
// Change 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;
}

// If you call the API using kotlin, note that the exception in the callback method must be nullable. Otherwise, the onFail method will not be called, as follows:
// The type of clientException is CosXmlClientException?, and the type of serviceException is 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 examples, see GitHub.

SDK API Reference

For detailed parameters and method descriptions of all SDK interfaces, see SDK API Reference.

API Operations

For the description of the API for simple operations, see the PUT Object - Copy document.
For the description of the APIs for multipart operations, see the List Multipart Uploads, Initiate Multipart Upload, Upload Part - Copy, List Parts, Abort Multipart Upload, and Complete Multipart Upload documents.
For the description of the API for deleting objects, see the DELETE Object document.

Ajuda e Suporte

Esta página foi útil?

comentários