This commit is contained in:
Rogelio
2025-10-13 18:16:25 +00:00
parent 739f087cef
commit 325f1ef439
415 changed files with 46870 additions and 0 deletions

View File

View File

@@ -0,0 +1,54 @@
from datetime import UTC, datetime, timedelta
from typing import Protocol
from azure.storage.blob import BlobSasPermissions, generate_blob_sas
from azure.storage.blob.aio import BlobServiceClient
class AzureStorage:
def __init__(self, connection_string: str | None):
if connection_string:
self.client = BlobServiceClient.from_connection_string(connection_string)
def _generate_sas_token(self, filename: str, bucket: str, minute_duration: int):
expiry_time = datetime.now(UTC) + timedelta(minutes=minute_duration)
token = generate_blob_sas(
account_name=self.client.account_name, # type: ignore
container_name=bucket,
blob_name=filename,
account_key=self.client.credential.account_key,
permission=BlobSasPermissions(read=True),
expiry=expiry_time,
)
return token
async def get_file_url(
self, filename: str, bucket: str, minute_duration: int, image: bool
) -> str | None:
if not hasattr(self, "client"):
return None
blob_client = self.client.get_blob_client(container=bucket, blob=filename)
exists = await blob_client.exists()
if exists:
sas_token = self._generate_sas_token(filename, bucket, minute_duration)
return f"{blob_client.url}?{sas_token}"
else:
return None
async def get_blob_bytes(self, bucket: str, filename: str):
if not hasattr(self, "client"):
raise ValueError("No connection string provided to AzureStorage object.")
blob_client = self.client.get_blob_client(container=bucket, blob=filename)
return (await blob_client.download_blob()).readall()
class Config(Protocol):
azure_blob_connection_string: str
@classmethod
def from_config(cls, c: Config):
return cls(c.azure_blob_connection_string)

View File

@@ -0,0 +1,15 @@
[project]
name = "azure-storage"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
authors = [{ name = "ajac-zero", email = "ajcardoza2000@gmail.com" }]
requires-python = ">=3.12"
dependencies = ["azure-storage-blob>=12.25.1"]
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.hatch.build.targets.wheel]
packages = ["banortegpt"]