x-cos-hash-crc64ecma header represents the CRC64 value of a part, which you can compare against the locally calculated CRC64 value to verify the part integrity.HTTP/1.1 200 OKcontent-length: 0connection: closedate: Thu, 05 Dec 2019 01:58:03 GMTetag: "358e8c8b1bfa35ee3bd44cb3d2cc416b"server: tencent-cosx-cos-hash-crc64ecma: 15060521397700495958x-cos-request-id: NWRlODY0MmJfMjBiNDU4NjRfNjkyZl80ZjZi****
x-cos-hash-crc64ecma header represents the CRC64 value of an entire object, which you can compare against the locally calculated CRC64 value to verify the object integrity.HTTP/1.1 200 OKcontent-type: application/xmltransfer-encoding: chunkedconnection: closedate: Thu, 05 Dec 2019 02:01:17 GMTserver: tencent-cosx-cos-hash-crc64ecma: 15060521397700495958x-cos-request-id: NWRlODY0ZWRfMjNiMjU4NjRfOGQ4Ml81MDEw****[Object Content]
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientfrom qcloud_cos import CosServiceErrorfrom qcloud_cos import CosClientErrorimport sysimport loggingimport hashlibimport crcmodlogging.basicConfig(level=logging.INFO, stream=sys.stdout)# Configure user attributes, including SecretId, SecretKey, and region# APPID has been removed from the configuration. Please specify it using the `Bucket` parameter in the format of `BucketName-APPID`.secret_id = COS_SECRETID # Replace with your own SecretIdsecret_key = COS_SECRETKEY # Replace with your own SecretKeyregion = 'ap-beijing' # Replace with your own region (which is Beijing in this sample)token = None # If a temporary key is used, the token needs to be specified. This is optional and is left empty by default.config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # Get the configured objectclient = CosS3Client(config)
OBJECT_PART_SIZE = 1024 * 1024 # Size of each simulated partOBJECT_TOTAL_SIZE = OBJECT_PART_SIZE * 1 + 123 # Total size of the objectobject_body = '1' * OBJECT_TOTAL_SIZE # Object content#Calculate the checksum of the entire object.c64 = crcmod.mkCrcFun(0x142F0E1EBA9EA3693, initCrc=0, xorOut=0xffffffffffffffff, rev=True)local_crc64 =str(c64(object_body))
# Initialize the multipart uploadresponse = client.create_multipart_upload(Bucket='examplebucket-1250000000', #Replace with your own bucket name and APPIDKey='exampleobject', # Replace with the key value of your uploaded objectStorageClass='STANDARD', # Storage class of the object)#Get the UploadId of the multipart uploadupload_id = response['UploadId']
#Upload an object in parts where the size of each part is OBJECT_PART_SIZE except the last part which may be smallerpart_list = list()position = 0left_size = OBJECT_TOTAL_SIZEpart_number = 0while left_size > 0:part_number += 1if left_size >= OBJECT_PART_SIZE:body = object_body[position:position+OBJECT_PART_SIZE]else:body = object_body[position:]position += OBJECT_PART_SIZEleft_size -= OBJECT_PART_SIZElocal_part_crc_64 = c64(body)#Calculate CRC64 locallyresponse = client.upload_part(Bucket='examplebucket-1250000000',Key='exampleobject',Body=body,PartNumber=part_number,UploadId=upload_id,)part_crc_64 = response['x-cos-hash-crc64ecma']# CRC64 returned by the serverif local_part_crc64 != part_crc_64:# Data Checkprint 'crc64 check FAIL'exit(-1)etag = response['ETag']part_list.append({'ETag' : etag, 'PartNumber' : part_number})
#Complete the multipart uploadresponse = client.complete_multipart_upload(Bucket='examplebucket-1250000000', #Replace with your own bucket name and APPIDKey=‘exampleobject’, #Key value of the objectUploadId=upload_id,MultipartUpload={ #Require one-to-one correspondence between ETag and PartNumber for each part'Part' : part_list},)crc64ecma = response['x-cos-hash-crc64ecma']if crc64ecma != local_crc64:# Data Checkprint 'check crc64 Failed'exit(-1)
String calculateCrc64(File localFile) throws IOException {CRC64 crc64 = new CRC64();try (FileInputStream stream = new FileInputStream(localFile)) {byte[] b = new byte[1024 * 1024];while (true) {final int read = stream.read(b);if (read <= 0) {break;}crc64.update(b, read);}}return Long.toUnsignedString(crc64.getValue());}
// For more information about how to create COSClient, see [Getting Started](https://www.tencentcloud.com/document/product/436/10199).ObjectMetadata cosMeta = COSClient().getObjectMetadata(bucketName, cosFilePath);String cosCrc64 = cosMeta.getCrc64Ecma();String localCrc64 = calculateCrc64(localFile);if (cosCrc64.equals(localCrc64)) {System.out.println("ok");} else {System.out.println("fail");}
Feedback