generated from ks6088ts/template-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from ks6088ts-labs/feature/issue-14_add-blob-s…
…ervice add blob service
- Loading branch information
Showing
14 changed files
with
355 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
BLOB_STORAGE_ACCOUNT_NAME="account_name" | ||
BLOB_STORAGE_CONTAINER_NAME="dev" | ||
BLOB_STORAGE_SAS_TOKEN="sp=racwdli&st=2024-10-17T23:32:36Z&se=2024-10-18T07:32:36Z&spr=https&sv=2022-11-02&sr=c&sig=CHANGE_ME" |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from logging import getLogger | ||
|
||
import pytest | ||
|
||
from tests.utilities import SKIP_TEST, client | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
@pytest.mark.skipif(SKIP_TEST, reason="need to launch the backend server first") | ||
def test_main(): | ||
path_format = "/blob_storage{0}" | ||
|
||
# list_images | ||
response = client.get( | ||
url=path_format.format("/images"), | ||
) | ||
assert response.status_code == 200 | ||
logger.info(f"response: {response.json()}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from logging import getLogger | ||
|
||
from azure.storage.blob import BlobServiceClient | ||
|
||
from workshop_azure_iot.settings.blob_storage import Settings | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
class Client: | ||
def __init__(self, settings: Settings) -> None: | ||
self.settings = settings | ||
self.blob_service_client = BlobServiceClient( | ||
account_url=f"https://{self.settings.blob_storage_account_name}.blob.core.windows.net", | ||
credential=self.settings.blob_storage_sas_token, | ||
) | ||
|
||
def download_blob_stream( | ||
self, | ||
blob_name: str, | ||
) -> bytes: | ||
blob_client = self.blob_service_client.get_blob_client( | ||
container=self.settings.blob_storage_container_name, | ||
blob=blob_name, | ||
) | ||
logger.info(f"Downloaded blob {blob_name} from container {self.settings.blob_storage_container_name}") | ||
return blob_client.download_blob().readall() | ||
|
||
def upload_blob_stream( | ||
self, | ||
blob_name: str, | ||
stream: bytes, | ||
) -> dict: | ||
blob_client = self.blob_service_client.get_blob_client( | ||
container=self.settings.blob_storage_container_name, | ||
blob=blob_name, | ||
) | ||
logger.info(f"Uploaded blob {blob_name} to container {self.settings.blob_storage_container_name}") | ||
return blob_client.upload_blob(stream, overwrite=True) | ||
|
||
def list_blobs( | ||
self, | ||
) -> list: | ||
container_client = self.blob_service_client.get_container_client(self.settings.blob_storage_container_name) | ||
logger.info(f"Listed blobs in container {self.settings.blob_storage_container_name}") | ||
return [blob.name for blob in container_client.list_blobs()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from logging import getLogger | ||
|
||
from fastapi import APIRouter, UploadFile, status | ||
from fastapi.responses import JSONResponse, Response | ||
|
||
from workshop_azure_iot.internals.blob_storage import Client | ||
from workshop_azure_iot.settings.blob_storage import Settings | ||
|
||
logger = getLogger(__name__) | ||
|
||
client = Client( | ||
settings=Settings(), | ||
) | ||
|
||
router = APIRouter( | ||
prefix="/blob_storage", | ||
tags=["blob_storage"], | ||
) | ||
|
||
|
||
@router.get( | ||
"/images/{blob_name}", | ||
responses={200: {"content": {"image/jpeg": {}}}}, | ||
response_class=Response, | ||
) | ||
async def get_image( | ||
blob_name: str, | ||
): | ||
image_bytes = client.download_blob_stream( | ||
blob_name=blob_name, | ||
) | ||
return Response( | ||
content=image_bytes, | ||
media_type="image/jpeg", | ||
) | ||
|
||
|
||
@router.get( | ||
"/images", | ||
) | ||
async def list_images(): | ||
return client.list_blobs() | ||
|
||
|
||
@router.post( | ||
"/images", | ||
status_code=201, | ||
) | ||
async def upload_image( | ||
file: UploadFile, | ||
blob_name: str, | ||
): | ||
content = await file.read() | ||
response = client.upload_blob_stream( | ||
blob_name=blob_name, | ||
stream=content, | ||
) | ||
logger.warning(f"Response: {response}, type: {type(response)}") | ||
return JSONResponse( | ||
status_code=status.HTTP_201_CREATED, | ||
content={ | ||
"message": "Image uploaded successfully", | ||
"etag": response.get("etag"), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
|
||
class Settings(BaseSettings): | ||
blob_storage_account_name: str | ||
blob_storage_container_name: str | ||
blob_storage_sas_token: str | ||
|
||
model_config = SettingsConfigDict(env_file="blob_storage.env") |