12345678910111213141516171819202122232425262728293031 |
- def upload_file(s3, bucketname, file_path):
- b = s3.get_bucket(bucketname)
- filename = os.path.basename(file_path)
- k = b.new_key(filename)
- mp = b.initiate_multipart_upload(filename)
- source_size = os.stat(file_path).st_size
- bytes_per_chunk = 5000*1024*1024
- chunks_count = int(math.ceil(source_size / float(bytes_per_chunk)))
- for i in range(chunks_count):
- offset = i * bytes_per_chunk
- remaining_bytes = source_size - offset
- bytes = min([bytes_per_chunk, remaining_bytes])
- part_num = i + 1
- print( "uploading part " + str(part_num) + " of " + str(chunks_count))
- with open(file_path, 'r') as fp:
- fp.seek(offset)
- mp.upload_part_from_file(fp=fp, part_num=part_num, size=bytes)
- if len(mp.get_all_parts()) == chunks_count:
- mp.complete_upload()
- print ("upload_file done")
- else:
- mp.cancel_upload()
- print ("upload_file failed")
|