endpoint = 'your endpoint'
access_key = 'your access key'
secret_key = 'your secret key'
bucket_name = 'bucketname'
def __init__(self, data):
self.crc64 = tos.utils.Crc64()
content = self.data.read(8 * 1024)
self.crc64.update(content)
file_name = 'usr/local/testfile.txt'
client = tos.TosClientV2(access_key, secret_key, endpoint, region, enable_crc=False)
total_size = os.path.getsize(file_name)
part_size = 1024 * 1024 * 5
resp = client.create_multipart_upload(bucket_name, key)
upload_id = resp.upload_id
crc64 = tos.utils.Crc64()
with open(file_name, 'rb') as f:
while offset < total_size:
num_to_upload = min(part_size, total_size - offset)
crc64_io = Crc64IO(SizeAdapter(f, num_to_upload, init_offset=offset))
part = client.upload_part(bucket_name, key, upload_id, part_number,
if crc64_io.crc64.crc != part.hash_crc64_ecma:
raise Exception('crc64 not match')
crc64_final = crc64.combine(crc64_final, crc64_io.crc64.crc, num_to_upload)
resp = client.complete_multipart_upload(bucket_name, key, upload_id, parts)
if crc64_final != resp.hash_crc64_ecma:
raise Exception('crc64 not match')
except tos.exceptions.TosClientError as e:
print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
except tos.exceptions.TosServerError as e:
print('fail with server error, code: {}'.format(e.code))
print('error with request id: {}'.format(e.request_id))
print('error with message: {}'.format(e.message))
print('error with http code: {}'.format(e.status_code))
print('error with ec: {}'.format(e.ec))
print('error with request url: {}'.format(e.request_url))
print('fail with unknown error: {}'.format(e))