-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from LeadingEDJE/task/12-upload-file
#12 - Add upload utility method
- Loading branch information
Showing
5 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import boto3 | ||
from boto3.exceptions import Boto3Error | ||
from botocore.exceptions import ClientError | ||
from stackmanager.exceptions import TransferError, ValidationError | ||
from stackmanager.messages import info | ||
|
||
|
||
class Uploader: | ||
"""Utility for uploading files to S3""" | ||
|
||
def __init__(self, client): | ||
self._client = client | ||
|
||
def upload(self, filename, bucket, key, acl='bucket-owner-full-control'): | ||
""" | ||
Uploads a local file to S3. | ||
:param filename: Name of local file | ||
:param bucket: S3 Bucket Name | ||
:param key: S3 Key | ||
:param acl: Object ACL, defaults to bucket-owner-full-control | ||
:raises ValidationError: If local file does not exist | ||
:raises TransferError: If there is an error uploading the file | ||
""" | ||
try: | ||
self._client.upload_file(Filename=filename, Bucket=bucket, Key=key, | ||
ExtraArgs={'ACL': acl}) | ||
info(f'\nUploaded {filename} to s3://{bucket}/{key}') | ||
except FileNotFoundError: | ||
raise ValidationError(f'File {filename} not found') | ||
except (Boto3Error, ClientError) as e: | ||
raise TransferError(e) | ||
|
||
|
||
def create_uploader(profile, region): | ||
""" | ||
Create a new Uploader | ||
:param profile: AWS Profile | ||
:param region: AWS Region | ||
:return: Configured Uploader | ||
""" | ||
session = boto3.Session(profile_name=profile, region_name=region) | ||
client = session.client('s3') | ||
return Uploader(client) |