This document provides an overview of APIs and SDK code samples related to object copy and movement.
Simple operations
API | Operation | Description |
---|---|---|
PUT Object - Copy | Copying an object (modifying object attributes) | Copies a file to a destination path. |
DELETE Object | Deleting an object | Deletes a specified object from a bucket. |
Multipart operations
API | Operation | Description |
---|---|---|
List Multipart Uploads | Querying multipart uploads/copy | Queries in-progress multipart uploads/copy. |
Initiate Multipart Upload | Initializing a multipart upload/copy operation | Initializes a multipart upload/copy operation. |
Upload Part - Copy | Copying a part | Copies an object as a part. |
List Parts | Querying uploaded/copied parts | Queries the uploaded/copied parts of a multipart operation. |
Complete Multipart Upload | Completing a multipart upload/copy | Completes the multipart upload/copy of a file. |
Abort Multipart Upload | Aborting a multipart upload/copy | Aborts a multipart operation and deletes the uploaded/copied parts. |
For the parameters and method descriptions of all the APIs in the SDK, see SDK API Reference.
The advanced APIs encapsulate async requests for the simple copy and multipart copy APIs and support pausing, resuming, and canceling copy requests.
// 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);
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 source object attributes
CopyObjectRequest.CopySourceStruct copySourceStruct =
new CopyObjectRequest.CopySourceStruct(
sourceAppid, sourceBucket, sourceRegion, sourceCosPath);
// Destination bucket
// 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";
// 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 the response callback
cosxmlCopyTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult result) {
COSXMLCopyTask.COSXMLCopyTaskResult copyResult =
(COSXMLCopyTask.COSXMLCopyTaskResult) 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
cosxmlCopyTask.setTransferStateListener(new TransferStateListener() {
@Override
public void onStateChanged(TransferState state) {
// todo notify transfer state
}
});
Object movement involves copying the source object to the target location and deleting the source object.
Since COS uses the bucket name (Bucket
) and object key (ObjectKey
) to identify objects, moving an object will change the object identifier. Currently, COS’s Android SDK does not provide a standalone API to change object identifiers. However, you can still move the object with a combination of basic operations (object copy and object delete).
For example, if you want to move the picture.jpg
object to the “doc” directory that is in the same bucket (mybucket-1250000000
), you can copy the picture.jpg
to the “doc” directory (making the object key doc/picture.jpg
) and then delete the source object.
Likewise, if you need to move picture.jpg
in the mybucket-1250000000
bucket to another bucket myanothorbucket-1250000000
, you can copy the object to the myanothorbucket-1250000000
bucket first and then delete the source object.
final String sourceAppid = "1250000000"; // Account appid
final String sourceBucket = "sourcebucket-1250000000"; // Bucket of the source object
final String sourceRegion = "COS_REGION"; // Region where the bucket of the source object resides
final String sourceKey = "sourceObject"; // Key of the source object
// Construct source object attributes
CopyObjectRequest.CopySourceStruct copySource = new CopyObjectRequest.CopySourceStruct(sourceAppid, sourceBucket,
sourceRegion, sourceKey);
String bucket = "examplebucket-1250000000"; // Destination bucket in the format of BucketName-APPID
String key = "exampleobject"; // Object key of the destination bucket
// 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 it is successfully copied.
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 samples, please visit GitHub.
This API (PUT Object-Copy) is used to copy an object to a destination path.
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);
// 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
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 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.
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);
// 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
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 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.
String appId = "1250000000"; // Account APPID
// 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 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;
}
// 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.
String appId = "1250000000"; // Account APPID
// 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 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;
}
// 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.
Apakah halaman ini membantu?