Skip to content

Instantly share code, notes, and snippets.

@wellflat
Last active August 12, 2020 06:50
Show Gist options
  • Save wellflat/0d9f676a6bf18ca2f3908345577de5ea to your computer and use it in GitHub Desktop.
Save wellflat/0d9f676a6bf18ca2f3908345577de5ea to your computer and use it in GitHub Desktop.
AWS S3 multipart upload sample
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import botocore
from boto3.session import Session
from boto3.s3.transfer import TransferConfig
from pprint import pprint
class S3Client(object):
def __init__(self, bucket):
self.session = Session(
aws_access_key_id='access key',
aws_secret_access_key='secret key',
region_name='region'
)
self.s3 = self.session.resource('s3')
self.bucket = bucket
def upload(self, filename, key):
try:
config = TransferConfig(
multipart_threshold=5*1024**3, #5GB
max_concurrency=10,
num_download_attempts=10
)
#"https://stackoverflow.com/questions/49444424/multipart-upload-using-boto3
#http://boto3.readthedocs.io/en/latest/guide/s3.html#uploads
self.s3.Bucket(self.bucket).upload_file(filename, key, Config=config)
except FileNotFoundError as e:
print(e)
except botocore.exceptions.EndpointConnectionError as e:
print(e)
def list_objects(self):
for obj in self.s3.Bucket(self.bucket).objects.all():
print(obj.key)
if __name__ == '__main__':
bucket = 'test-bucket'
client = S3Client(bucket)
filename = 'test.tar.gz'
key = 'test'
client.upload(filename, key)
client.list_objects()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment