Skip to content

Commit 02a6292

Browse files
committed
add internals
1 parent 3c5dd00 commit 02a6292

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

backend/internals/azure_storage.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from logging import getLogger
2+
3+
from azure.storage.blob import BlobServiceClient
4+
5+
from backend.settings import azure_storage as azure_storage_settings
6+
7+
logger = getLogger(__name__)
8+
9+
10+
class BlobStorageClient:
11+
def __init__(self, settings: azure_storage_settings.Settings):
12+
self.settings = settings
13+
14+
def get_blob_service_client(self) -> BlobServiceClient:
15+
return BlobServiceClient(
16+
account_url=f"https://{self.settings.azure_storage_account_name}.blob.core.windows.net",
17+
credential=self.settings.azure_storage_sas_token,
18+
)
19+
20+
def upload_blob_stream(
21+
self,
22+
container_name: str,
23+
blob_name: str,
24+
stream: bytes,
25+
):
26+
blob_service_client = self.get_blob_service_client()
27+
blob_client = blob_service_client.get_blob_client(
28+
container=container_name,
29+
blob=blob_name,
30+
)
31+
blob_client.upload_blob(stream, overwrite=True)
32+
logger.info(f"Uploaded blob {blob_name} to container {container_name}")
33+
34+
def download_blob_stream(
35+
self,
36+
container_name: str,
37+
blob_name: str,
38+
) -> bytes:
39+
blob_service_client = self.get_blob_service_client()
40+
blob_client = blob_service_client.get_blob_client(
41+
container=container_name,
42+
blob=blob_name,
43+
)
44+
stream = blob_client.download_blob().readall()
45+
logger.info(f"Downloaded blob {blob_name} from container {container_name}")
46+
return stream
47+
48+
def delete_blob(
49+
self,
50+
container_name: str,
51+
blob_name: str,
52+
):
53+
blob_service_client = self.get_blob_service_client()
54+
blob_client = blob_service_client.get_blob_client(
55+
container=container_name,
56+
blob=blob_name,
57+
)
58+
blob_client.delete_blob()
59+
logger.info(f"Deleted blob {blob_name} from container {container_name}")
60+
61+
def list_blobs(
62+
self,
63+
container_name: str,
64+
) -> list:
65+
blob_service_client = self.get_blob_service_client()
66+
container_client = blob_service_client.get_container_client(container_name)
67+
return container_client.list_blobs()

0 commit comments

Comments
 (0)