srx-lib-azure 0.1.2__py3-none-any.whl → 0.1.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.

Potentially problematic release.


This version of srx-lib-azure might be problematic. Click here for more details.

srx_lib_azure/blob.py CHANGED
@@ -1,4 +1,5 @@
1
1
  import os
2
+ import tempfile
2
3
  from datetime import datetime, timedelta, timezone
3
4
  from typing import Optional, BinaryIO, Tuple
4
5
 
@@ -111,3 +112,77 @@ class AzureBlobService:
111
112
  logger.error(f"Failed to upload stream to {blob_path}: {e}")
112
113
  return None
113
114
 
115
+ async def download_file(self, blob_path: str) -> Optional[bytes]:
116
+ """Download a blob's content as bytes."""
117
+ if not self.connection_string:
118
+ logger.error("Azure Storage connection string not configured")
119
+ return None
120
+ try:
121
+ client = self._get_blob_service()
122
+ container = client.get_container_client(self.container_name)
123
+ blob_client = container.get_blob_client(blob_path)
124
+ download_stream = blob_client.download_blob()
125
+ content = download_stream.readall()
126
+ logger.info(f"Successfully downloaded {blob_path}")
127
+ return content
128
+ except Exception as e:
129
+ logger.error(f"Failed to download {blob_path}: {e}")
130
+ return None
131
+
132
+ async def download_to_temp_file(self, blob_path: str) -> Optional[str]:
133
+ """Download a blob to a temporary file and return its path."""
134
+ content = await self.download_file(blob_path)
135
+ if content is None:
136
+ return None
137
+ try:
138
+ with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(blob_path)[1]) as tf:
139
+ tf.write(content)
140
+ path = tf.name
141
+ logger.info(f"Downloaded {blob_path} to temporary file: {path}")
142
+ return path
143
+ except Exception as e:
144
+ logger.error(f"Failed to create temporary file for {blob_path}: {e}")
145
+ return None
146
+
147
+ def get_blob_url(self, blob_path: str, generate_sas: bool = True) -> Optional[str]:
148
+ """Get a direct URL for a blob; optionally generate a SAS URL."""
149
+ if generate_sas:
150
+ try:
151
+ return self._generate_sas_url(blob_path)
152
+ except Exception as e:
153
+ logger.error(f"Failed to generate SAS URL for {blob_path}: {e}")
154
+ return None
155
+ if self.base_blob_url:
156
+ return f"{self.base_blob_url.rstrip('/')}/{blob_path}"
157
+ logger.error("Cannot generate blob URL without base URL")
158
+ return None
159
+
160
+ async def delete_file(self, blob_path: str) -> bool:
161
+ """Delete a blob and return True on success."""
162
+ if not self.connection_string:
163
+ logger.error("Azure Storage connection string not configured")
164
+ return False
165
+ try:
166
+ client = self._get_blob_service()
167
+ container = client.get_container_client(self.container_name)
168
+ blob_client = container.get_blob_client(blob_path)
169
+ blob_client.delete_blob()
170
+ logger.info(f"Successfully deleted {blob_path}")
171
+ return True
172
+ except Exception as e:
173
+ logger.error(f"Failed to delete {blob_path}: {e}")
174
+ return False
175
+
176
+ async def file_exists(self, blob_path: str) -> bool:
177
+ """Check if a blob exists in the container."""
178
+ if not self.connection_string:
179
+ logger.error("Azure Storage connection string not configured")
180
+ return False
181
+ try:
182
+ client = self._get_blob_service()
183
+ container = client.get_container_client(self.container_name)
184
+ blob_client = container.get_blob_client(blob_path)
185
+ return blob_client.exists()
186
+ except Exception as e:
187
+ logger.error(f"Failed to check existence of {blob_path}: {e}")
188
+ return False
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: srx-lib-azure
3
- Version: 0.1.2
3
+ Version: 0.1.4
4
4
  Summary: Azure helpers for SRX services: Blob, Email, Table
5
5
  Author-email: SRX <dev@srx.id>
6
6
  Requires-Python: >=3.12
@@ -0,0 +1,7 @@
1
+ srx_lib_azure/__init__.py,sha256=K0UCmkKw7HWJMshp6Xv3SxD4y26r7bdcPtb_2aRc2rs,174
2
+ srx_lib_azure/blob.py,sha256=uCsRUCQN4GHtlyLBtDUvy0_mZaFTxWKdCA407cRIU8I,8245
3
+ srx_lib_azure/email.py,sha256=H8KCnYFuQ2dKzpWx3BsKv9tVCV-pEmm7vXUJkOnpVh4,1719
4
+ srx_lib_azure/table.py,sha256=_5DCsk1SLqCc27F7469hxnRASS3XeffqK_MsJE1cD7Y,3022
5
+ srx_lib_azure-0.1.4.dist-info/METADATA,sha256=P_eY6gpsYaolhdOp3eraiqlTq5ePnsr22K5VzDjcJYk,1600
6
+ srx_lib_azure-0.1.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
+ srx_lib_azure-0.1.4.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- srx_lib_azure/__init__.py,sha256=K0UCmkKw7HWJMshp6Xv3SxD4y26r7bdcPtb_2aRc2rs,174
2
- srx_lib_azure/blob.py,sha256=Y720DmrfYkyZIXLSGDwp_fPmU0ZKa-cAxM5-KdeWC5Q,4886
3
- srx_lib_azure/email.py,sha256=H8KCnYFuQ2dKzpWx3BsKv9tVCV-pEmm7vXUJkOnpVh4,1719
4
- srx_lib_azure/table.py,sha256=_5DCsk1SLqCc27F7469hxnRASS3XeffqK_MsJE1cD7Y,3022
5
- srx_lib_azure-0.1.2.dist-info/METADATA,sha256=9ZxUBkAXwWgHejaGse890yA5i9dWpvTu1qthrfn7IaA,1600
6
- srx_lib_azure-0.1.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
- srx_lib_azure-0.1.2.dist-info/RECORD,,