boto3-assist 0.2.2__py3-none-any.whl → 0.2.4__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.
boto3_assist/s3/s3.py CHANGED
@@ -7,6 +7,7 @@ MIT License. See Project Root for the license information.
7
7
  import os
8
8
  import tempfile
9
9
  import time
10
+ import io
10
11
  from typing import Any, Dict, List, Optional
11
12
 
12
13
  from aws_lambda_powertools import Logger
@@ -50,6 +51,19 @@ class S3(S3Connection):
50
51
  aws_secret_access_key=aws_secret_access_key,
51
52
  )
52
53
 
54
+ def create_bucket(self, bucket_name: str) -> None:
55
+ """
56
+ Create an S3 bucket
57
+ :param bucket_name: Bucket to create
58
+ :return: True if bucket is created, else False
59
+ """
60
+ try:
61
+ self.client.create_bucket(Bucket=bucket_name)
62
+ logger.info(f"Bucket {bucket_name} created")
63
+ except ClientError as e:
64
+ logger.exception(e)
65
+ raise e
66
+
53
67
  def generate_presigned_url(
54
68
  self,
55
69
  bucket_name: str,
@@ -139,7 +153,7 @@ class S3(S3Connection):
139
153
 
140
154
  return response
141
155
 
142
- def upload_file_obj(self, bucket: str, key: str, file_obj: bytes) -> str:
156
+ def upload_file_obj(self, bucket: str, key: str, file_obj: bytes | str) -> str:
143
157
  """
144
158
  Uploads a file object to s3. Returns the full s3 path s3://<bucket>/<key>
145
159
  """
@@ -156,7 +170,13 @@ class S3(S3Connection):
156
170
  }
157
171
  )
158
172
  try:
159
- self.client.upload_fileobj(Fileobj=file_obj, Bucket=bucket, Key=key)
173
+ # convert if necessary
174
+ file_obj: bytes = (
175
+ file_obj.encode("utf-8") if isinstance(file_obj, str) else file_obj
176
+ )
177
+ self.client.upload_fileobj(
178
+ Fileobj=io.BytesIO(file_obj), Bucket=bucket, Key=key
179
+ )
160
180
 
161
181
  except ClientError as ce:
162
182
  error = {
@@ -503,3 +523,19 @@ class S3(S3Connection):
503
523
  else:
504
524
  # Not in AWS Lambda, use the system's default temp directory
505
525
  return tempfile.gettempdir()
526
+
527
+ def encode(
528
+ self, text: str, encoding: str = "utf-8", errors: str = "strict"
529
+ ) -> bytes:
530
+ """
531
+ Encodes a string for s3
532
+ """
533
+ return text.encode(encoding=encoding, errors=errors)
534
+
535
+ def decode(
536
+ self, file_obj: bytes, encoding: str = "utf-8", errors: str = "strict"
537
+ ) -> str:
538
+ """
539
+ Decodes bytes to a string
540
+ """
541
+ return file_obj.decode(encoding=encoding, errors=errors)
@@ -58,6 +58,16 @@ class FileOperations:
58
58
  dirname = os.path.dirname(path)
59
59
  return dirname
60
60
 
61
+ @staticmethod
62
+ def read_file(path: str, encoding: str = "utf-8") -> str:
63
+ """
64
+ Read a file
65
+ """
66
+ logger.debug(f"reading file {path}")
67
+ with open(path, "r", encoding=encoding) as file:
68
+ data = file.read()
69
+ return data
70
+
61
71
  @staticmethod
62
72
  def write_to_file(path: str, data: str, append: bool = False) -> str:
63
73
  """
boto3_assist/version.py CHANGED
@@ -1 +1 @@
1
- __version__ = '0.2.2'
1
+ __version__ = '0.2.4'
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: boto3_assist
3
- Version: 0.2.2
3
+ Version: 0.2.4
4
4
  Summary: Additional boto3 wrappers to make your life a little easier
5
5
  Author-email: Eric Wilson <boto3-assist@geekcafe.com>
6
6
  Classifier: License :: Other/Proprietary License
@@ -2,7 +2,7 @@ boto3_assist/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  boto3_assist/boto3session.py,sha256=NWhWtYR3143thEbTpoklkwdz77-fTMs-QsoQdqfRm6E,6430
3
3
  boto3_assist/connection.py,sha256=EJlGueLIYqMSKs7aQlThK1S0Zkb8dOYBWch1iRZdgUI,3233
4
4
  boto3_assist/connection_tracker.py,sha256=_s1t7h2DOi3CCIHIr_HIKyGjku65WR-HJ_v8vJHDvO0,2977
5
- boto3_assist/version.py,sha256=QQ0yjZQnvXM7JeLLZEZWX6NmVUPu1lSROWOXF49_gug,22
5
+ boto3_assist/version.py,sha256=N5-p8dQB8uwuCMpS1ADLf_E6rvWovtRRp3vY9Cq2gw4,22
6
6
  boto3_assist/cloudwatch/cloudwatch_connection.py,sha256=mnGWaLSQpHh5EeY7Ek_2o9JKHJxOELIYtQVMX1IaHn4,2480
7
7
  boto3_assist/cloudwatch/cloudwatch_connection_tracker.py,sha256=mzRtO1uHrcfJNh1XrGEiXdTqxwEP8d1RqJkraMNkgK0,410
8
8
  boto3_assist/cloudwatch/cloudwatch_log_connection.py,sha256=qQMZHjUJ6gA8wU9utjQhOURXNSPH2RjxSoAy83bvoCs,1737
@@ -28,16 +28,16 @@ boto3_assist/environment_services/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeR
28
28
  boto3_assist/environment_services/environment_loader.py,sha256=jvG1xwMtgkZqu70NbjG1b1IefKiWgaidjZZoqsSfULk,3370
29
29
  boto3_assist/environment_services/environment_variables.py,sha256=4ccBKdPt6O7hcRT3zBHd8vqu8yQU8udmoD5RLAT3iMs,6801
30
30
  boto3_assist/errors/custom_exceptions.py,sha256=zC2V2Y4PUtKj3uLPn8mB-JessksKWJWvKM9kp1dmvt8,760
31
- boto3_assist/s3/s3.py,sha256=j-u-gLxCDHS4XFEsFGSdfr3QBUxqc95g0NBmQQ5Z1g0,15965
31
+ boto3_assist/s3/s3.py,sha256=NdfoU_vcg8ypyC2iYKm236zYTB3V7ave5vc_6Mdscxc,17065
32
32
  boto3_assist/s3/s3_connection.py,sha256=fMD1FxQazJ9gSFDrRmrv7YJE5QnCesGoWC1b5Dba_Jo,3774
33
33
  boto3_assist/utilities/datetime_utility.py,sha256=TbqGQkJDTahqvaZAIV550nhYnW1Bsq0Hdu3Go6P4RRs,10282
34
- boto3_assist/utilities/file_operations.py,sha256=HWQR6o3R5JaISixJS8CpUMsiNgW3Qp5yVZ9KA-jhPmA,3133
34
+ boto3_assist/utilities/file_operations.py,sha256=Zy8fu8fpuVNf7U9NimrLdy5FRF71XSI159cnRdzmzGY,3411
35
35
  boto3_assist/utilities/http_utility.py,sha256=koFv7Va-8ng-47Nt1K2Sh7Ti95e62IYs9VMLlGh9Kt4,1173
36
36
  boto3_assist/utilities/logging_utility.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
37
37
  boto3_assist/utilities/serialization_utility.py,sha256=s_QQRIhtwIE7xN5nU13mNk2wtWyErBX_Sg7n0gbHj-M,4308
38
38
  boto3_assist/utilities/string_utility.py,sha256=w8l063UT3GE48tuJopETyZrjG4CgAzWkyDWMAYMg5Og,7432
39
- boto3_assist-0.2.2.dist-info/METADATA,sha256=6oHYMJUemswTElxmH2QeESelggP2rLyppiMc-vm7oBQ,1742
40
- boto3_assist-0.2.2.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
41
- boto3_assist-0.2.2.dist-info/licenses/LICENSE-EXPLAINED.txt,sha256=WFREvTpfTjPjDHpOLADxJpCKpIla3Ht87RUUGii4ODU,606
42
- boto3_assist-0.2.2.dist-info/licenses/LICENSE.txt,sha256=PXDhFWS5L5aOTkVhNvoitHKbAkgxqMI2uUPQyrnXGiI,1105
43
- boto3_assist-0.2.2.dist-info/RECORD,,
39
+ boto3_assist-0.2.4.dist-info/METADATA,sha256=6-R1661w1pMbKim2BiW3_Fq_rdVte73z5FM9oTmgyI4,1742
40
+ boto3_assist-0.2.4.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
41
+ boto3_assist-0.2.4.dist-info/licenses/LICENSE-EXPLAINED.txt,sha256=WFREvTpfTjPjDHpOLADxJpCKpIla3Ht87RUUGii4ODU,606
42
+ boto3_assist-0.2.4.dist-info/licenses/LICENSE.txt,sha256=PXDhFWS5L5aOTkVhNvoitHKbAkgxqMI2uUPQyrnXGiI,1105
43
+ boto3_assist-0.2.4.dist-info/RECORD,,