-
Notifications
You must be signed in to change notification settings - Fork 4
/
s3.py
53 lines (41 loc) · 1.29 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
import boto3
from harvester import bucket_name
# ruff: noqa: F841
def create_s3_client(s3_config):
"""create boto3.client object
s3_config (dict) : configuration dict.
"""
try:
return boto3.client("s3", **s3_config)
except ValueError as e:
return e
def create_s3_upload_data(body, key_name, content_type):
"""create s3 data to be uploaded to the default bucket
json_str (str) : data to be placed in s3 bucket as json string.
key_name (str) : name of the file to be placed in the s3 bucket.
"""
return {
"Body": body,
"Bucket": bucket_name,
"Key": key_name,
"ContentType": content_type,
}
def delete_s3_object(S3, bucket_name, object_key):
try:
return S3.delete_object(Bucket=bucket_name, Key=object_key)
except Exception as e:
pass
def get_s3_object(S3, bucket_name, object_key):
try:
return S3.get_object(Bucket=bucket_name, Key=object_key)
except Exception as e:
pass
def upload_to_S3(S3, s3_upload_data):
"""store the s3 payload
S3 (boto3 client) : boto3 S3 client
s3_upload_data (dict) : payload to be stored in s3 bucket.
"""
try:
return S3.put_object(**s3_upload_data)
except Exception as e:
pass