-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathawsutil.py
96 lines (82 loc) · 2.09 KB
/
awsutil.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#
# awsutil.py
#
# Helper functions that interact with AWS S3.
#
# Original author:
# Prof. Joe Hummel
# Northwestern University
#
import boto3
import logging
import uuid
import pathlib
###################################################################
#
# download_file
#
# ref: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/bucket/download_file.html
#
def download_file(bucket, key):
"""
Downloads a file from an S3 bucket
Parameters
----------
bucket : S3 bucket to download from,
key : object's name in bucket
Returns
-------
filename of downloaded file or None upon an error
"""
try:
#
# generate a unique filename:
#
filename = str(uuid.uuid4())
extension = pathlib.Path(key).suffix
filename += extension
#
# downoad:
#
bucket.download_file(key, filename)
#
return filename
except Exception as e:
logging.error("awss3.download_file() failed:")
logging.error(e)
return None
###################################################################
#
# upload_file
#
# ref: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/bucket/upload_file.html
#
def upload_file(local_filename, bucket, key):
"""
Uploads a file to an S3 bucket, setting the content type to "image/jpeg" if
a jpg file and the permissions to be publicly readable
Parameters
----------
local_filename : name of local file to upload,
bucket : S3 Bucket to upload to,
key : object's name in the bucket after upload
Returns
-------
key that was passed in or None upon an error
"""
try:
if key.endswith('jpg'): # image file
content_type = 'image/jpeg'
else: # default:
content_type = 'application/octet-stream'
bucket.upload_file(local_filename,
key,
ExtraArgs={
'ACL': 'public-read',
'ContentType': content_type
})
return key
except Exception as e:
logging.error("awss3.upload_file() failed:")
logging.error(e)
return None