릴리스 노트
제품 공지
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****
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)# SecretId, SecretKey, Region을 포함한 사용자 속성을 설정합니다.# APPID는 설정에서 삭제되었으니 매개변수 Bucket에 APPID를 입력하십시오. Bucket은 BucketName-APPID로 구성됩니다.secret_id = COS_SECRETID # 사용자 SecretId 정보로 대체secret_key = COS_SECRETKEY # 사용자 SecretKey 정보로 대체region = 'ap-beijing' # 사용자 Region으로 대체, 예시는 베이징 리전token = None # 임시 키를 사용할 경우 Token 입력, 기본값이 null이면 입력하지 않음config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 설정 객체 획득client = CosS3Client(config)
OBJECT_PART_SIZE = 1024 * 1024 #각 멀티파트 크기 시뮬레이션OBJECT_TOTAL_SIZE = OBJECT_PART_SIZE * 1 + 123 #총 객체 크기object_body = '1' * OBJECT_TOTAL_SIZE #객체 콘텐츠#전체 객체의 crc64 검사 값 계산c64 = crcmod.mkCrcFun(0x142F0E1EBA9EA3693L, initCrc=0L, xorOut=0xffffffffffffffffL, rev=True)local_crc64 =str(c64(object_body))
#멀티파트 업로드 초기화response = client.create_multipart_upload(Bucket='examplebucket-1250000000', #사용자 Bucket 이름으로 대체. examplebucket은 예시 버킷이며, 1250000000은 예시 APPID임.Key='exampleobject', #업로드한 객체의 Key 값으로 대체StorageClass='STANDARD', #객체의 스토리지 유형)#멀티파트 업로드한 UploadId 획득upload_id = response['UploadId']
#객체를 멀티파트 업로드할 때, 각 파트의 크기는 OBJECT_PART_SIZE이며, 마지막 파트의 크기는 OBJECT_PART_SIZE보다 작을 수 있습니다.part_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_crc64 = str(c64(body)) #로컬에서 CRC64 계산response = client.upload_part(Bucket='examplebucket-1250000000',Key='exampleobject',Body=body,PartNumber=part_number,UploadId=upload_id,)part_crc_64 = response['x-cos-hash-crc64ecma']# 서버에서 반환된 CRC64if local_part_crc64 != part_crc_64: # 데이터 검사print 'crc64 check FAIL'exit(-1)etag = response['ETag']part_list.append({'ETag' : etag, 'PartNumber' : part_number})
#멀티파트 업로드 완료response = client.complete_multipart_upload(Bucket='examplebucket-1250000000', #사용자 Bucket 이름으로 대체. examplebucket은 예시 버킷이며, 1250000000은 예시 APPID임.Key='exampleobject', #객체의 Key 값UploadId=upload_id,MultipartUpload={ #모든 파트의 ETag와 PartNumber가 모두 대응되어야 함'Part' : part_list},)crc64ecma = response['x-cos-hash-crc64ecma']if crc64ecma != local_crc64: # 데이터 검사print '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());}
// COSClient 생성 참고: [시작하기](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");}
피드백