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.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: my_aws_helpers
3
- Version: 1.4.0
3
+ Version: 1.5.0
4
4
  Summary: AWS Helpers
5
5
  Home-page: https://github.com/JarrodMccarthy/aws_helpers.git
6
6
  Author: Jarrod McCarthy
@@ -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,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: my_aws_helpers
3
- Version: 1.4.0
3
+ Version: 1.5.0
4
4
  Summary: AWS Helpers
5
5
  Home-page: https://github.com/JarrodMccarthy/aws_helpers.git
6
6
  Author: Jarrod McCarthy
@@ -3,7 +3,7 @@ from setuptools import find_namespace_packages, setup
3
3
 
4
4
  base_path = os.path.abspath(os.path.dirname(__file__))
5
5
 
6
- version = "1.4.0"
6
+ version = "1.5.0"
7
7
 
8
8
  setup(
9
9
  name="my_aws_helpers",
@@ -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