// 1. Initialize TransferService. You should use the same TransferService for the same configurationTransferConfig transferConfig = new TransferConfig.Builder().build();TransferService transferService = new TransferService(cosXmlService, transferConfig);// 2. Initialize PutObjectRequest// 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"; // Location identifier of the object in the bucket, i.e., the object keyString srcPath = "examplefilepath"; // Absolute path to the local filePutObjectRequest putObjectRequest = new PutObjectRequest(bucket,cosPath, srcPath);// 3. Call the upload method to upload the filefinal COSUploadTask uploadTask = transferService.upload(putObjectRequest);uploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {// You can get the CRC64 value of the file after the upload is successfulString crc64 = result.getHeader("x-cos-hash-crc64ecma");}// 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();}}});
// 1. Initialize TransferService. You should use the same TransferService for the same configurationTransferConfig transferConfig = new TransferConfig.Builder().build();TransferService transferService = new TransferService(cosXmlService, transferConfig);// 2. Initialize GetObjectRequest// 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"; // Location identifier of the object in the bucket, i.e., the object keyString savePathDir = context.getCacheDir().toString(); // Local directory path// File name saved locally. If not specified (null), it will be the same as the COS file nameString savedFileName = "exampleobject";GetObjectRequest getObjectRequest = new GetObjectRequest(bucket,cosPath, savePathDir, savedFileName);// 3. Call the download method to download the filefinal COSDownloadTask downloadTask = transferService.download(getObjectRequest);downloadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {// You can get the CRC64 value of the file after the download is successfulString cosCRC64 = result.getHeader("x-cos-hash-crc64ecma");}// 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();}}});
TransferService
for upload or download, the SDK verifies the data by default. If you still want to perform CRC64 check yourself, refer to the following code.// 1. Refer to the above upload or download request sample code to get the CRC64 value of the file on COSString cosCRC64 = "examplecoscrc64";// 2. Calculate the CRC64 value of the local fileFile localFile = new File("examplefilepath");String localCRC64 = DigestUtils.getCRC64String(localFile);// 3. Check whether localCRC64 and cosCRC64 are the sameif (localCRC64.equals(cosCRC64)) {// CRC64 values are the same}
Apakah halaman ini membantu?