API | Operation | Description |
Copying an object (modifying object attributes) | Copies a file to a destination path. | |
Deleting an object | Deletes a specified object from a bucket. |
API | Operation | Description |
Querying multipart uploads/copy | Queries in-progress multipart uploads/copy. | |
Initializing a multipart upload/copy operation | Initializes a multipart upload/copy operation. | |
Copying a part | Copies an object as a part. | |
Querying uploaded/copied parts | Queries the uploaded/copied parts of a multipart operation. | |
Completing a multipart upload/copy | Completes the multipart upload/copy of a file. | |
Aborting a multipart upload/copy | Aborts a multipart operation and deletes the uploaded/copied parts. |
// 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 TransferManagerTransferManager transferManager = new TransferManager(cosXmlService,transferConfig);String sourceAppid = "1250000000"; // Account APPIDString sourceBucket = "sourcebucket-1250000000"; // Bucket of the source objectString sourceRegion = "COS_REGION"; // Region where the bucket of the source object residesString sourceCosPath = "sourceObject"; // Key of the source object// Construct source object attributesCopyObjectRequest.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/bucketString bucket = "examplebucket-1250000000";// Destination objectString cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object key// Copy the objectCOSXMLCopyTask cosxmlCopyTask = transferManager.copy(bucket, cosPath,copySourceStruct);// Set the response callbackcosxmlCopyTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic 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?@Overridepublic 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 progresscosxmlCopyTask.setTransferStateListener(new TransferStateListener() {@Overridepublic void onStateChanged(TransferState state) {// todo notify transfer state}});
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).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.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 appidfinal String sourceBucket = "sourcebucket-1250000000"; // Bucket of the source objectfinal String sourceRegion = "COS_REGION"; // Region where the bucket of the source object residesfinal String sourceKey = "sourceObject"; // Key of the source object// Construct source object attributesCopyObjectRequest.CopySourceStruct copySource = new CopyObjectRequest.CopySourceStruct(sourceAppid, sourceBucket,sourceRegion, sourceKey);String bucket = "examplebucket-1250000000"; // Destination bucket in the format of BucketName-APPIDString 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() {@Overridepublic 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();}}@Overridepublic void onFail(CosXmlRequest request, CosXmlClientException exception, CosXmlServiceException serviceException) {}});
String sourceAppid = "1250000000"; // Account APPIDString sourceBucket = "sourcebucket-1250000000"; // Bucket of the source objectString sourceRegion = "COS_REGION"; // Region where the bucket of the source object residesString sourceCosPath = "sourceObject"; // Key of the source object// Construct the source object attributesCopyObjectRequest.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/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object keyCopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, cosPath,copySourceStruct);cosXmlService.copyObjectAsync(copyObjectRequest, new CosXmlResultListener() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
String sourceAppid = "1250000000"; // Account APPIDString sourceBucket = "sourcebucket-1250000000"; // Bucket of the source objectString sourceRegion = "COS_REGION"; // Region where the bucket of the source object residesString sourceCosPath = "sourceObject"; // Key of the source object// Construct the source object attributesCopyObjectRequest.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/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object keyCopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, cosPath,copySourceStruct);copyObjectRequest.setCopyMetaDataDirective(MetaDataDirective.REPLACED);copyObjectRequest.setXCOSMeta("x-cos-metadata-oldKey", "newValue");cosXmlService.copyObjectAsync(copyObjectRequest, new CosXmlResultListener() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
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/bucketString bucket = "examplebucket-1250000000";String region = "COS_REGION"; // Region where the bucket of the source object residesString cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object key// Construct the source object attributesCopyObjectRequest.CopySourceStruct copySourceStruct =new CopyObjectRequest.CopySourceStruct(appId, bucket, region, cosPath);CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, cosPath,copySourceStruct);copyObjectRequest.setCopyMetaDataDirective(MetaDataDirective.REPLACED);// Replace metadatacopyObjectRequest.setXCOSMeta("x-cos-metadata-oldKey", "newValue");cosXmlService.copyObjectAsync(copyObjectRequest, new CosXmlResultListener() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
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/bucketString bucket = "examplebucket-1250000000";String region = "COS_REGION"; // Region where the bucket of the source object residesString cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object key// Construct the source object attributesCopyObjectRequest.CopySourceStruct copySourceStruct =new CopyObjectRequest.CopySourceStruct(appId, bucket, region, cosPath);CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, cosPath,copySourceStruct);// Set the storage class to STANDARD_IAcopyObjectRequest.setCosStorageClass(COSStorageClass.STANDARD_IA);cosXmlService.copyObjectAsync(copyObjectRequest, new CosXmlResultListener() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
Was this page helpful?