Skip to content

Instantly share code, notes, and snippets.

@itsTeknas
Created October 2, 2021 15:38
Show Gist options
  • Save itsTeknas/8cbcabc1f7acc0e241951537c7d0ce03 to your computer and use it in GitHub Desktop.
Save itsTeknas/8cbcabc1f7acc0e241951537c7d0ce03 to your computer and use it in GitHub Desktop.
Upload a file to AWS S3 with multipart uploads using the boto3 python client.
# https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html
import boto3
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html
s3Client = boto3.client('s3')
s3 = boto3.resource('s3')
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.create_multipart_upload
multipart_upload = s3Client.create_multipart_upload(
ACL='public-read',
Bucket='multipart-using-boto',
ContentType='video/mp4',
Key='movie.mp4',
)
print("UploadId: ", multipart_upload['UploadId'])
part_number = 1
parts = []
with open('movie.mp4', 'rb') as f:
while True:
piece = f.read(10000000) # roughly 10 mb parts
if piece == b'':
break # end of file
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#multipartuploadpart
uploadPart = s3.MultipartUploadPart(
'multipart-using-boto', 'movie.mp4', multipart_upload['UploadId'], part_number)
uploadPartResponse = uploadPart.upload(
Body=piece,
)
print('uploaded part ', part_number, ' with Etag ', uploadPartResponse['ETag'])
parts.append({
'PartNumber': part_number,
'ETag': uploadPartResponse['ETag']
})
part_number += 1
print(parts)
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.complete_multipart_upload
completeResult = s3Client.complete_multipart_upload(
Bucket='multipart-using-boto',
Key='movie.mp4',
MultipartUpload={
'Parts': parts
},
UploadId=multipart_upload['UploadId'],
)
print(completeResult)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment