my-aws-helpers 1.4.0__tar.gz → 1.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of my-aws-helpers might be problematic. Click here for more details.
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/PKG-INFO +1 -1
- my_aws_helpers-1.5.0/my_aws_helpers/s3.py +115 -0
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/my_aws_helpers.egg-info/PKG-INFO +1 -1
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/setup.py +1 -1
- my_aws_helpers-1.4.0/my_aws_helpers/s3.py +0 -47
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/README.md +0 -0
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/my_aws_helpers/api.py +0 -0
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/my_aws_helpers/cognito.py +0 -0
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/my_aws_helpers/dynamo.py +0 -0
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/my_aws_helpers/errors.py +0 -0
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/my_aws_helpers/sfn.py +0 -0
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/my_aws_helpers.egg-info/SOURCES.txt +0 -0
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/my_aws_helpers.egg-info/dependency_links.txt +0 -0
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/my_aws_helpers.egg-info/requires.txt +0 -0
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/my_aws_helpers.egg-info/top_level.txt +0 -0
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/my_aws_helpers.egg-info/zip-safe +0 -0
- {my_aws_helpers-1.4.0 → my_aws_helpers-1.5.0}/setup.cfg +0 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import boto3
|
|
2
|
+
import io
|
|
3
|
+
import json
|
|
4
|
+
from typing import Tuple, Optional
|
|
5
|
+
from copy import copy
|
|
6
|
+
import os
|
|
7
|
+
import gzip
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class S3Location:
|
|
12
|
+
bucket: str
|
|
13
|
+
file_name: str
|
|
14
|
+
location: str
|
|
15
|
+
|
|
16
|
+
@classmethod
|
|
17
|
+
def from_location(cls, location: str):
|
|
18
|
+
bucket, file_name = location.split('/')[0], '/'.join(location.split('/')[1:])
|
|
19
|
+
return cls(bucket = bucket, file_name = file_name)
|
|
20
|
+
|
|
21
|
+
def __init__(self, bucket: str, file_name: str) -> None:
|
|
22
|
+
self.bucket = bucket
|
|
23
|
+
self.file_name = file_name
|
|
24
|
+
self.location = f"{self.bucket}/{self.file_name}"
|
|
25
|
+
|
|
26
|
+
def serialise(self):
|
|
27
|
+
return copy(vars(self))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class S3:
|
|
31
|
+
client: Optional[boto3.client]
|
|
32
|
+
|
|
33
|
+
def __init__(self, client: Optional[boto3.client]) -> None:
|
|
34
|
+
self.client = client if client else self._get_client()
|
|
35
|
+
|
|
36
|
+
def _get_client(self) -> boto3.client:
|
|
37
|
+
region_name = os.environ["AWS_DEFAULT_REGION"]
|
|
38
|
+
s3_client = boto3.client("s3", region_name=region_name)
|
|
39
|
+
endpoint_url = s3_client.meta.endpoint_url
|
|
40
|
+
s3_client = boto3.client("s3", region_name=region_name, endpoint_url=endpoint_url)
|
|
41
|
+
return s3_client
|
|
42
|
+
|
|
43
|
+
def _streaming_body_to_dict(self, payload):
|
|
44
|
+
file_like_obj = io.BytesIO(payload.read())
|
|
45
|
+
response = json.loads(file_like_obj.getvalue())
|
|
46
|
+
return response
|
|
47
|
+
|
|
48
|
+
def put_json_object(self, bucket_name: str, file_name: str, object: dict):
|
|
49
|
+
return self.client.put_object(
|
|
50
|
+
Body = json.dumps(object),
|
|
51
|
+
Bucket = bucket_name,
|
|
52
|
+
Key = file_name
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
def get_object(self, bucket_name: str, file_name: str):
|
|
56
|
+
response = self.client.get_object(
|
|
57
|
+
Bucket = bucket_name,
|
|
58
|
+
Key = file_name
|
|
59
|
+
)
|
|
60
|
+
return self._streaming_body_to_dict(response["Body"])
|
|
61
|
+
|
|
62
|
+
def get_presigned_url(self, bucket_name: str, file_name: str, expires_in: int = 3600):
|
|
63
|
+
return self.client.generate_presigned_url(
|
|
64
|
+
'get_object',
|
|
65
|
+
Params = {
|
|
66
|
+
"Bucket": bucket_name,
|
|
67
|
+
"Key": file_name,
|
|
68
|
+
},
|
|
69
|
+
ExpiresIn = expires_in
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
def get_s3_location_from_bucket_file(bucket_name: str, file_name: str) -> S3Location:
|
|
73
|
+
return S3Location(bucket=bucket_name, file_name=file_name)
|
|
74
|
+
|
|
75
|
+
def get_bucket_file_from_s3_location(s3_location: S3Location) -> S3Location:
|
|
76
|
+
return S3Location.from_location(location=s3_location)
|
|
77
|
+
|
|
78
|
+
def save_document_content(
|
|
79
|
+
self,
|
|
80
|
+
file_contents: bytes,
|
|
81
|
+
file_name: str,
|
|
82
|
+
bucket_name: str,
|
|
83
|
+
content_encoding: str = "",
|
|
84
|
+
content_type: str = "application/pdf",
|
|
85
|
+
compress: bool = True,
|
|
86
|
+
) -> S3Location:
|
|
87
|
+
"""
|
|
88
|
+
saves document content to bucket, in file_name
|
|
89
|
+
Options for content_type:
|
|
90
|
+
"application/pdf"
|
|
91
|
+
"text/plain"
|
|
92
|
+
"application/json"
|
|
93
|
+
probably more
|
|
94
|
+
Options for content_encoding:
|
|
95
|
+
"": default encoding
|
|
96
|
+
"gzip": compressed contents
|
|
97
|
+
"""
|
|
98
|
+
if compress or file_name.endswith(".gz"):
|
|
99
|
+
file_contents = gzip.compress(file_contents)
|
|
100
|
+
content_encoding = "gzip"
|
|
101
|
+
obj = self.client.Object(bucket_name, file_name)
|
|
102
|
+
obj.put(Body = file_contents, ContentType = content_type, ContentEncoding = content_encoding)
|
|
103
|
+
return S3Location(bucket=bucket_name, file_name=file_name)
|
|
104
|
+
|
|
105
|
+
def read_binary_from_s3(self, obj) -> bytes:
|
|
106
|
+
d_bytes = io.BytesIO()
|
|
107
|
+
obj.download_fileobj(d_bytes)
|
|
108
|
+
d_bytes.seek(0)
|
|
109
|
+
if obj.content_encoding == "gzip":
|
|
110
|
+
try:
|
|
111
|
+
with gzip.GzipFile(fileobj=d_bytes) as gz_file:
|
|
112
|
+
return gz_file.read()
|
|
113
|
+
except gzip.BadGzipFile:
|
|
114
|
+
d_bytes.seek(0)
|
|
115
|
+
return d_bytes.read()
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import boto3
|
|
2
|
-
import io
|
|
3
|
-
import json
|
|
4
|
-
from typing import Tuple
|
|
5
|
-
|
|
6
|
-
class S3:
|
|
7
|
-
client: boto3.client
|
|
8
|
-
|
|
9
|
-
def __init__(self) -> None:
|
|
10
|
-
self.client = boto3.client('s3')
|
|
11
|
-
|
|
12
|
-
def _streaming_body_to_dict(self, payload):
|
|
13
|
-
file_like_obj = io.BytesIO(payload.read())
|
|
14
|
-
response = json.loads(file_like_obj.getvalue())
|
|
15
|
-
return response
|
|
16
|
-
|
|
17
|
-
def put_json_object(self, bucket_name: str, file_name: str, object: dict):
|
|
18
|
-
return self.client.put_object(
|
|
19
|
-
Body = json.dumps(object),
|
|
20
|
-
Bucket = bucket_name,
|
|
21
|
-
Key = file_name
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
def get_object(self, bucket_name: str, file_name: str):
|
|
25
|
-
response = self.client.get_object(
|
|
26
|
-
Bucket = bucket_name,
|
|
27
|
-
Key = file_name
|
|
28
|
-
)
|
|
29
|
-
return self._streaming_body_to_dict(response["Body"])
|
|
30
|
-
|
|
31
|
-
def get_presigned_url(self, bucket_name: str, file_name: str, expires_in: int = 3600):
|
|
32
|
-
return self.client.generate_presigned_url(
|
|
33
|
-
'get_object',
|
|
34
|
-
Params = {
|
|
35
|
-
"Bucket": bucket_name,
|
|
36
|
-
"Key": file_name,
|
|
37
|
-
},
|
|
38
|
-
ExpiresIn = expires_in
|
|
39
|
-
)
|
|
40
|
-
|
|
41
|
-
def get_s3_location_from_bucket_file(bucket_name: str, file_name: str) -> str:
|
|
42
|
-
return f"{bucket_name}/{file_name}"
|
|
43
|
-
|
|
44
|
-
def get_bucket_file_from_s3_location(s3_location: str) -> Tuple[str, str]:
|
|
45
|
-
bucket, file_name = s3_location.split('/')[0], "/".join(s3_location.split('/')[1:])
|
|
46
|
-
return bucket, file_name
|
|
47
|
-
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|