-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.py
55 lines (48 loc) · 1.92 KB
/
s3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import logging
import boto3
from pathlib import Path
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
class s3Wrapper:
"""Encapsulates S3 object actions."""
def __init__(self, s3_resource: type[boto3]) -> None:
"""
:param s3_resource: A Boto3 s3 low-level service client
"""
self.s3_resource = s3_resource
def upload(self, file: str, bucket_name: str) -> None:
"""
Upload a file.
:param file_name: The file to upload including the path to the file to upload.
:param bucket_name: The name of the bucket.
"""
try:
key = Path(file).name
except FileNotFoundError:
logger.exception(f"No such file: '{file}'.")
raise
try:
self.s3_resource.upload_file(Filename=file, Bucket=bucket_name, Key=key)
waiter = self.s3_resource.get_waiter('bucket_exists')
waiter.wait(Bucket=bucket_name, WaiterConfig={'Delay': 5, 'MaxAttempts': 10})
logger.info(f"Upload object {key} to bucket {bucket_name}.")
except ClientError:
logger.exception("Couldn't upload object {key} to bucket {bucket_name}.")
raise
def download(self, file: str, bucket_name: str) -> None:
"""
Download the file_name
:param file: The file to download including the path to the file to download to.
:param bucket_name: The name of the bucket.
"""
try:
key = Path(file).name
except FileNotFoundError:
logger.exception(f"No such file: '{file}'.")
raise
try:
self.s3_resource.download_file(Filename=file, Bucket=bucket_name, Key=key)
logger.info(f"Download file {key} from bucket {bucket_name}.")
except ClientError:
logger.exception(f"Couldn't download file {key} from bucket {bucket_name}.")
raise