cos:PutObject and the action for the source object to cos:GetObject. For more authorization information, see CAM-supported business APIs.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.cos:DeleteObject. For more authorization information, see CAM-supported business APIs.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. |
// 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 TransferManagerTransferManager transferManager = new TransferManager(cosXmlService,transferConfig);
String sourceBucket = "sourcebucket-1250000000"; // The bucket where the source object residesString sourceRegion = "COS_REGION"; // The region where the source object's bucket residesString sourceCosPath = "sourceObject"; // The object key of the source object// Construct source object propertiesCopyObjectRequest.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/bucketString bucket = "examplebucket-1250000000";// Target objectString cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object key.// Copy objectCOSXMLCopyTask cosxmlCopyTask = transferManager.copy(bucket, cosPath,copySourceStruct);// Set the result callbackcosxmlCopyTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic 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?@Overridepublic 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() {@Overridepublic void onStateChanged(TransferState state) {// todo notify transfer state}});
final String sourceBucket = "sourcebucket-1250000000"; // The bucket where the source object residesfinal String sourceRegion = "COS_REGION"; // The region where the source object's bucket residesfinal String sourceKey = "sourceObject"; // The key of the source object// Construct source object propertiesCopyObjectRequest.CopySourceStruct copySource = new CopyObjectRequest.CopySourceStruct(sourceBucket,sourceRegion, sourceKey);String bucket = "examplebucket-1250000000"; // The target bucket. Format: BucketName-APPIDString 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() {@Overridepublic 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();}}@Overridepublic void onFail(CosXmlRequest request, CosXmlClientException exception, CosXmlServiceException serviceException) {}});
String sourceBucket = "sourcebucket-1250000000"; // The bucket where the source object residesString sourceRegion = "COS_REGION"; // The region where the source object's bucket residesString sourceCosPath = "sourceObject"; // The key of the source object// Construct source object propertiesCopyObjectRequest.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/bucketString 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() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
String sourceBucket = "sourcebucket-1250000000"; // The bucket where the source object residesString sourceRegion = "COS_REGION"; // The region where the source object's bucket residesString sourceCosPath = "sourceObject"; // The key of the source object// Construct source object propertiesCopyObjectRequest.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/bucketString 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() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// 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/bucketString bucket = "examplebucket-1250000000";String region = "COS_REGION"; // The region where the source object's bucket residesString cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object key.// Construct source object propertiesCopyObjectRequest.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 valuecopyObjectRequest.setXCOSMeta("x-cos-metadata-oldKey", "newValue");cosXmlService.copyObjectAsync(copyObjectRequest, new CosXmlResultListener() {@Overridepublic 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// 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/bucketString bucket = "examplebucket-1250000000";String region = "COS_REGION"; // The region where the source object's bucket residesString cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object key.// Construct source object propertiesCopyObjectRequest.CopySourceStruct copySourceStruct =new CopyObjectRequest.CopySourceStruct(bucket, region, cosPath);CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, cosPath,copySourceStruct);// Change 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 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?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
Esta página foi útil?
Você também pode entrar em contato com a Equipe de vendas ou Enviar um tíquete em caso de ajuda.
comentários