import os
import math
import oss2
from tqdm import tqdm
auth = oss2.Auth('', '')
bucket_name = ''
object_name = ''
file_path = ''
bucket = oss2.Bucket(auth, 'http://oss-cn-beijing.aliyuncs.com', bucket_name)
upload_id = None
parts = []
if os.path.exists(object_name + '.part'):
with open(object_name + '.part', 'r') as f:
upload_id = f.readline().strip()
for line in f.readlines():
if line.strip() == '':
continue
part_number, part_etag = line.strip().split('\t')
parts.append(oss2.models.PartInfo(int(part_number), part_etag))
if upload_id is None:
upload_id = bucket.init_multipart_upload(object_name).upload_id
part_size = 50 * 1024 * 1024
with open(file_path, 'rb') as f:
index = len(parts) + 1
file_size = os.path.getsize(file_path)
part_count = int(math.ceil(file_size / part_size))
with tqdm(total=file_size, unit='B', unit_scale=True, unit_divisor=1024) as pbar:
while True:
data = f.read(part_size)
if not data:
break
part = bucket.upload_part(
object_name,
upload_id,
index,
data
)
parts.append(oss2.models.PartInfo(index, part.etag))
uploaded_size = index * part_size
if uploaded_size > file_size:
uploaded_size = file_size
pbar.update(uploaded_size - pbar.n)
index += 1
with open(object_name + '.part', 'w') as f:
f.write(upload_id + '\n')
for part in parts:
f.write('{}\t{}\n'.format(part.part_number, part.etag))
result = bucket.complete_multipart_upload(
object_name,
upload_id,
parts
)
print('Upload successfully!')