Created
June 2, 2019 02:51
-
-
Save EuniceMadya/ab15210a3dc6515de346ee94dbdaeb5e to your computer and use it in GitHub Desktop.
S3 upload (multipart upload multipartupload)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this is a simplified version of seeing how to upload | |
# referencing from https://gist.github.com/teasherm/bb73f21ed2f3b46bc1c2ca48ec2c1cf5 | |
import boto3 | |
import os | |
from boto3.s3.transfer import TransferConfig | |
def upload_to_s3(filename, path): | |
bucket_name = 'BUCKET_name' #should be a bucket name | |
s3 = boto3.client('s3') | |
print("size of the file is " ,os.path.getsize(path)) | |
if(os.path.getsize(path) < 1024 * 1024 * 5): | |
s3.upload_file(path, bucket_name, filename) # multipart upload does not support multipart upload for a file less than 5MB | |
return "size is too small for multipart upload" | |
multipart_upload = s3.create_multipart_upload(Bucket=bucket_name,Key=filename) | |
parts = upload_parts(multipart_upload["UploadId"], path, filename) | |
result = complete(multipart_upload["UploadId"], parts, filename) | |
print(result) | |
return " none" | |
def upload_parts(mpu_id, path, filename): | |
parts = [] | |
uploaded_bytes = 0 | |
with open(path, "rb") as f: | |
i = 1 | |
while True: | |
data = f.read(1024 * 1024 * 5) # a part must be larger than 5MB, except for the last one | |
if not len(data): | |
break | |
part = s3.upload_part( | |
Body=data, Bucket=bucket_name, Key=filename, UploadId=mpu_id, PartNumber=i) | |
parts.append({"PartNumber": i, "ETag": part["ETag"]}) | |
uploaded_bytes += len(data) | |
i += 1 | |
return parts | |
def complete(mpu_id, parts, filename): | |
result = s3.complete_multipart_upload( | |
Bucket=bucket_name, | |
Key=filename, | |
UploadId=mpu_id, | |
MultipartUpload={"Parts": parts}) | |
return result | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment