-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathS3handler.py
More file actions
45 lines (36 loc) · 1.15 KB
/
Copy pathS3handler.py
File metadata and controls
45 lines (36 loc) · 1.15 KB
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
from config import aws_access_key_id
from config import aws_secret_access_key
from config import s3_bucket
import boto3
s3 = boto3.client(
"s3",
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key
)
def upload_file_to_s3(file, filename, content_type, bucket_name=s3_bucket, acl="public-read"):
"""
Docs: http://boto3.readthedocs.io/en/latest/guide/s3.html
"""
try:
s3.upload_fileobj(
file,
bucket_name,
filename,
ExtraArgs={
"ACL": acl,
"ContentType": content_type
}
)
except Exception as e:
print("Something Happened: ", e)
return e
return f"https://{bucket_name}.s3.amazonaws.com/{filename}"
def get_file(filename, bucket_name=s3_bucket):
response = s3.get_object(Bucket=bucket_name, Key=filename)
return response
def check_file(filename, bucket_name=s3_bucket):
response = s3.list_objects_v2(Bucket=bucket_name, Prefix=filename, MaxKeys=1)
for obj in response.get("Contents", []):
if obj["Key"] == filename:
return True
return False