Spaces:
Sleeping
Sleeping
update blob config:
Browse files- config/constant.py +1 -0
- externals/storages/azure_blob.py +25 -20
- services/uploader/azure_blob_service.py +13 -11
config/constant.py
CHANGED
|
@@ -23,6 +23,7 @@ class AzureBlobConstants:
|
|
| 23 |
ENDPOINT: str = os.environ.get("azureai__container__endpoint")
|
| 24 |
CONTAINER_NAME: str = os.environ.get("azureai__container__name")
|
| 25 |
SAS_KEY: str = os.environ.get("azureai__search__sas")
|
|
|
|
| 26 |
MAX_FILE_SIZE_MB: int = int(os.getenv("azureai__max_file_size_mb", "5"))
|
| 27 |
CHUNK_SIZE: int = 4 * 1024 * 1024
|
| 28 |
ACCOUNT_NAME: str = os.environ.get("azureai__container__account__name")
|
|
|
|
| 23 |
ENDPOINT: str = os.environ.get("azureai__container__endpoint")
|
| 24 |
CONTAINER_NAME: str = os.environ.get("azureai__container__name")
|
| 25 |
SAS_KEY: str = os.environ.get("azureai__search__sas")
|
| 26 |
+
CONTAINER_KEY: str = os.environ.get("azureai__container__key")
|
| 27 |
MAX_FILE_SIZE_MB: int = int(os.getenv("azureai__max_file_size_mb", "5"))
|
| 28 |
CHUNK_SIZE: int = 4 * 1024 * 1024
|
| 29 |
ACCOUNT_NAME: str = os.environ.get("azureai__container__account__name")
|
externals/storages/azure_blob.py
CHANGED
|
@@ -4,38 +4,43 @@ from typing import List, Dict, Any
|
|
| 4 |
|
| 5 |
from azure.identity.aio import DefaultAzureCredential
|
| 6 |
from azure.storage.blob.aio import BlobServiceClient, ContainerClient
|
| 7 |
-
from
|
|
|
|
| 8 |
|
| 9 |
from config.constant import AzureBlobConstants
|
|
|
|
| 10 |
from utils.logger import get_logger
|
| 11 |
|
| 12 |
logger = get_logger("azure-blob")
|
| 13 |
|
| 14 |
# ---------- SINGLETONS ----------
|
| 15 |
-
# _blob_service_client: BlobServiceClient | None = None
|
| 16 |
-
# _credential: DefaultAzureCredential | None = None
|
| 17 |
-
|
| 18 |
if "azureai__container__key" not in os.environ:
|
| 19 |
credential=DefaultAzureCredential()
|
| 20 |
-
logger.info(f"β
Initialized Azure AI Cred: Using Default Credential")
|
| 21 |
else:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# credential = AzureKeyCredential(AzureBlobConstants.SAS_KEY)
|
| 25 |
-
credential = AzureBlobConstants.SAS_KEY
|
| 26 |
-
logger.info(f"β
Initialized Azure AI Cred: Using Azure Key Credential")
|
| 27 |
|
| 28 |
|
| 29 |
-
async def get_blob_service_client() -> BlobServiceClient:
|
| 30 |
-
_blob_service_client = BlobServiceClient(
|
| 31 |
-
account_url=AzureBlobConstants.ENDPOINT,
|
| 32 |
-
credential=credential,
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
if _blob_service_client is not None:
|
| 36 |
-
logger.info("β
Azure BlobServiceClient initialized")
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
async def get_container_client() -> ContainerClient:
|
|
@@ -111,7 +116,7 @@ def helper_path_maker(
|
|
| 111 |
return f"{in_folder}/{tenant_id}/{user_id}/{filename}"
|
| 112 |
|
| 113 |
|
| 114 |
-
|
| 115 |
|
| 116 |
async def upload_pdf(file: UploadFile, tenant_id: str, user_id: str) -> dict:
|
| 117 |
container_client = await get_container_client()
|
|
|
|
| 4 |
|
| 5 |
from azure.identity.aio import DefaultAzureCredential
|
| 6 |
from azure.storage.blob.aio import BlobServiceClient, ContainerClient
|
| 7 |
+
from azure.core.credentials import AzureSasCredential
|
| 8 |
+
from azure.storage.blob import ContentSettings
|
| 9 |
|
| 10 |
from config.constant import AzureBlobConstants
|
| 11 |
+
from fastapi import UploadFile, HTTPException, status
|
| 12 |
from utils.logger import get_logger
|
| 13 |
|
| 14 |
logger = get_logger("azure-blob")
|
| 15 |
|
| 16 |
# ---------- SINGLETONS ----------
|
|
|
|
|
|
|
|
|
|
| 17 |
if "azureai__container__key" not in os.environ:
|
| 18 |
credential=DefaultAzureCredential()
|
| 19 |
+
logger.info(f"β
Initialized Azure AI Cred: Using **Default Credential**")
|
| 20 |
else:
|
| 21 |
+
credential = AzureSasCredential(AzureBlobConstants.SAS_KEY)
|
| 22 |
+
logger.info(f"β
Initialized Azure AI Cred: Using **Azure Key Credential**")
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
async def get_blob_service_client() -> BlobServiceClient:
|
| 27 |
+
try:
|
| 28 |
+
# default_credential = DefaultAzureCredential()
|
| 29 |
+
# blob_service_client = BlobServiceClient(url, credential=credential)
|
| 30 |
+
_blob_service_client = BlobServiceClient(
|
| 31 |
+
account_url=AzureBlobConstants.ENDPOINT,
|
| 32 |
+
credential=credential,
|
| 33 |
+
)
|
| 34 |
+
return _blob_service_client
|
| 35 |
+
except Exception as E:
|
| 36 |
+
logger.error(f'β Getting blob service client error, {E}')
|
| 37 |
+
logger.error(f'β οΈ using DefaultAzureCredential')
|
| 38 |
+
credential=DefaultAzureCredential()
|
| 39 |
+
_blob_service_client = BlobServiceClient(
|
| 40 |
+
account_url=AzureBlobConstants.ENDPOINT,
|
| 41 |
+
credential=credential,
|
| 42 |
+
)
|
| 43 |
+
return _blob_service_client
|
| 44 |
|
| 45 |
|
| 46 |
async def get_container_client() -> ContainerClient:
|
|
|
|
| 116 |
return f"{in_folder}/{tenant_id}/{user_id}/{filename}"
|
| 117 |
|
| 118 |
|
| 119 |
+
|
| 120 |
|
| 121 |
async def upload_pdf(file: UploadFile, tenant_id: str, user_id: str) -> dict:
|
| 122 |
container_client = await get_container_client()
|
services/uploader/azure_blob_service.py
CHANGED
|
@@ -6,6 +6,9 @@ dotenv.load_dotenv(EnvFilepath.ENVPATH)
|
|
| 6 |
from config.constant import AzureBlobConstants
|
| 7 |
from azure.identity import DefaultAzureCredential
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
from azure.storage.blob.aio import BlobServiceClient, ContainerClient
|
| 10 |
# from azure.storage.blob import BlobServiceClient, ContainerClient
|
| 11 |
from fastapi import UploadFile
|
|
@@ -18,19 +21,12 @@ logger = get_logger("azure blob")
|
|
| 18 |
|
| 19 |
|
| 20 |
# --- Environment Variables ---
|
| 21 |
-
# NOTE: Set the AZURE_STORAGE_CONNECTION_STRING in your environment (e.g., .env file or production environment).
|
| 22 |
-
# CONNECTION_STRING = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
|
| 23 |
-
# CONTAINER_NAME = "pdf-uploads" # The name of your Azure Blob Storage container
|
| 24 |
-
|
| 25 |
-
|
| 26 |
if "azureai__container__key" not in os.environ:
|
| 27 |
credential=DefaultAzureCredential()
|
| 28 |
-
logger.info(f"β
Initialized Azure AI Cred: Using Default Credential")
|
| 29 |
else:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
credential = AzureBlobConstants.SAS_KEY
|
| 33 |
-
logger.info(f"β
Initialized Azure AI Cred: Using Azure Key Credential")
|
| 34 |
|
| 35 |
|
| 36 |
async def get_blob_service_client() -> BlobServiceClient:
|
|
@@ -44,7 +40,13 @@ async def get_blob_service_client() -> BlobServiceClient:
|
|
| 44 |
return _blob_service_client
|
| 45 |
except Exception as E:
|
| 46 |
logger.error(f'β Getting blob service client error, {E}')
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# async def get_blob_service_client() -> BlobServiceClient:
|
| 50 |
# try:
|
|
|
|
| 6 |
from config.constant import AzureBlobConstants
|
| 7 |
from azure.identity import DefaultAzureCredential
|
| 8 |
|
| 9 |
+
from azure.core.credentials import AzureSasCredential
|
| 10 |
+
|
| 11 |
+
# from azure.storage.blob import BlobServiceClient, ContainerClient
|
| 12 |
from azure.storage.blob.aio import BlobServiceClient, ContainerClient
|
| 13 |
# from azure.storage.blob import BlobServiceClient, ContainerClient
|
| 14 |
from fastapi import UploadFile
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
# --- Environment Variables ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
if "azureai__container__key" not in os.environ:
|
| 25 |
credential=DefaultAzureCredential()
|
| 26 |
+
logger.info(f"β
Initialized Azure AI Cred: Using **Default Credential**")
|
| 27 |
else:
|
| 28 |
+
credential = AzureSasCredential(AzureBlobConstants.SAS_KEY)
|
| 29 |
+
logger.info(f"β
Initialized Azure AI Cred: Using **Azure Key Credential**")
|
|
|
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
async def get_blob_service_client() -> BlobServiceClient:
|
|
|
|
| 40 |
return _blob_service_client
|
| 41 |
except Exception as E:
|
| 42 |
logger.error(f'β Getting blob service client error, {E}')
|
| 43 |
+
logger.error(f'β οΈ using DefaultAzureCredential')
|
| 44 |
+
credential=DefaultAzureCredential()
|
| 45 |
+
_blob_service_client = BlobServiceClient(
|
| 46 |
+
account_url=AzureBlobConstants.ENDPOINT,
|
| 47 |
+
credential=credential,
|
| 48 |
+
)
|
| 49 |
+
return _blob_service_client
|
| 50 |
|
| 51 |
# async def get_blob_service_client() -> BlobServiceClient:
|
| 52 |
# try:
|