AWS S3 multipart upload sample
This file contains 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
#!/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