my-aws-helpers 1.4.0__py2.py3-none-any.whl → 1.6.0__py2.py3-none-any.whl

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.
my_aws_helpers/s3.py CHANGED
@@ -1,13 +1,44 @@
1
1
  import boto3
2
2
  import io
3
3
  import json
4
- from typing import Tuple
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
+
5
29
 
6
30
  class S3:
7
- client: boto3.client
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()
8
35
 
9
- def __init__(self) -> None:
10
- self.client = boto3.client('s3')
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
11
42
 
12
43
  def _streaming_body_to_dict(self, payload):
13
44
  file_like_obj = io.BytesIO(payload.read())
@@ -38,10 +69,48 @@ class S3:
38
69
  ExpiresIn = expires_in
39
70
  )
40
71
 
41
- def get_s3_location_from_bucket_file(bucket_name: str, file_name: str) -> str:
42
- return f"{bucket_name}/{file_name}"
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)
43
104
 
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
-
105
+ def read_binary_from_s3(self, s3_location: S3Location) -> bytes:
106
+ obj = self.client.Object(s3_location.bucket, s3_location.file_name)
107
+ d_bytes = io.BytesIO()
108
+ obj.download_fileobj(d_bytes)
109
+ d_bytes.seek(0)
110
+ if obj.content_encoding == "gzip":
111
+ try:
112
+ with gzip.GzipFile(fileobj=d_bytes) as gz_file:
113
+ return gz_file.read()
114
+ except gzip.BadGzipFile:
115
+ d_bytes.seek(0)
116
+ return d_bytes.read()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: my_aws_helpers
3
- Version: 1.4.0
3
+ Version: 1.6.0
4
4
  Summary: AWS Helpers
5
5
  Home-page: https://github.com/JarrodMccarthy/aws_helpers.git
6
6
  Author: Jarrod McCarthy
@@ -10,5 +10,5 @@ Classifier: Programming Language :: Python
10
10
  Classifier: Programming Language :: Python :: 3.8
11
11
  Classifier: Programming Language :: Python :: 3.9
12
12
  Classifier: Programming Language :: Python :: 3.10
13
- Requires-Dist: boto3 ==1.34.36
13
+ Requires-Dist: boto3==1.34.36
14
14
 
@@ -2,10 +2,10 @@ my_aws_helpers/api.py,sha256=ph-MjSYl7mg_I1fN7S2DBRgQS90Spa8ARBiSZ6trliE,2330
2
2
  my_aws_helpers/cognito.py,sha256=gSaPncfzvnYJykf-Q9Mv8LGwJssynMBYL6LhOuF89_M,6447
3
3
  my_aws_helpers/dynamo.py,sha256=klDKoDujIuitp6c_yc1EWHkS67GJpXbck1ymhEgIMhE,4059
4
4
  my_aws_helpers/errors.py,sha256=_SCvVGOPYrkhOAxkO22u1cASGcBL8afHtg3xwX_FmY0,156
5
- my_aws_helpers/s3.py,sha256=k006EWFNcuIwaI351PZxBEHaodiTBJrrz8F4-pv8_28,1559
5
+ my_aws_helpers/s3.py,sha256=79lBRxbs4-pkUlg3Pysx8YE9RHCdgfo1Y9-xxvNwrug,4062
6
6
  my_aws_helpers/sfn.py,sha256=YBuOMAhmCP2GzPMSDJ7bDe38eI7m9m5uiaeP6mwy3_Y,1848
7
- my_aws_helpers-1.4.0.dist-info/METADATA,sha256=iooMUki-dVS4EVewwHN8wJQ1ecW7_FXP6SxJsbH_FE0,453
8
- my_aws_helpers-1.4.0.dist-info/WHEEL,sha256=-G_t0oGuE7UD0DrSpVZnq1hHMBV9DD2XkS5v7XpmTnk,110
9
- my_aws_helpers-1.4.0.dist-info/top_level.txt,sha256=RK29MPT4_whrlRXrq_mXuB1Mo_xIyCI5BZc6QeP0olQ,15
10
- my_aws_helpers-1.4.0.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
11
- my_aws_helpers-1.4.0.dist-info/RECORD,,
7
+ my_aws_helpers-1.6.0.dist-info/METADATA,sha256=-cP27fz-MRMBtb2Yk07gb1tMCWv-uzDHQIj9dkozfQw,452
8
+ my_aws_helpers-1.6.0.dist-info/WHEEL,sha256=0VNUDWQJzfRahYI3neAhz2UVbRCtztpN5dPHAGvmGXc,109
9
+ my_aws_helpers-1.6.0.dist-info/top_level.txt,sha256=RK29MPT4_whrlRXrq_mXuB1Mo_xIyCI5BZc6QeP0olQ,15
10
+ my_aws_helpers-1.6.0.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
11
+ my_aws_helpers-1.6.0.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.42.0)
2
+ Generator: setuptools (75.5.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py2-none-any
5
5
  Tag: py3-none-any