dify/api/extensions/storage/azure_blob_storage.py

74 lines
3.0 KiB
Python
Raw Normal View History

2024-04-29 18:22:03 +08:00
from collections.abc import Generator
from datetime import datetime, timedelta, timezone
from azure.storage.blob import AccountSasPermissions, BlobServiceClient, ResourceTypes, generate_account_sas
from configs import dify_config
2024-06-04 13:04:56 +08:00
from extensions.ext_redis import redis_client
2024-04-29 18:22:03 +08:00
from extensions.storage.base_storage import BaseStorage
class AzureBlobStorage(BaseStorage):
"""Implementation for Azure Blob storage."""
2024-06-04 13:04:56 +08:00
def __init__(self):
super().__init__()
self.bucket_name = dify_config.AZURE_BLOB_CONTAINER_NAME
self.account_url = dify_config.AZURE_BLOB_ACCOUNT_URL
self.account_name = dify_config.AZURE_BLOB_ACCOUNT_NAME
self.account_key = dify_config.AZURE_BLOB_ACCOUNT_KEY
2024-06-04 13:04:56 +08:00
2024-04-29 18:22:03 +08:00
def save(self, filename, data):
2024-06-04 13:04:56 +08:00
client = self._sync_client()
blob_container = client.get_container_client(container=self.bucket_name)
2024-04-29 18:22:03 +08:00
blob_container.upload_blob(filename, data)
def load_once(self, filename: str) -> bytes:
2024-06-04 13:04:56 +08:00
client = self._sync_client()
blob = client.get_container_client(container=self.bucket_name)
2024-04-29 18:22:03 +08:00
blob = blob.get_blob_client(blob=filename)
data = blob.download_blob().readall()
return data
def load_stream(self, filename: str) -> Generator:
2024-06-04 13:04:56 +08:00
client = self._sync_client()
blob = client.get_blob_client(container=self.bucket_name, blob=filename)
blob_data = blob.download_blob()
yield from blob_data.chunks()
2024-04-29 18:22:03 +08:00
def download(self, filename, target_filepath):
2024-06-04 13:04:56 +08:00
client = self._sync_client()
blob = client.get_blob_client(container=self.bucket_name, blob=filename)
2024-04-29 18:22:03 +08:00
with open(target_filepath, "wb") as my_blob:
blob_data = blob.download_blob()
blob_data.readinto(my_blob)
def exists(self, filename):
2024-06-04 13:04:56 +08:00
client = self._sync_client()
blob = client.get_blob_client(container=self.bucket_name, blob=filename)
2024-04-29 18:22:03 +08:00
return blob.exists()
def delete(self, filename):
2024-06-04 13:04:56 +08:00
client = self._sync_client()
blob_container = client.get_container_client(container=self.bucket_name)
blob_container.delete_blob(filename)
def _sync_client(self):
cache_key = "azure_blob_sas_token_{}_{}".format(self.account_name, self.account_key)
2024-06-04 13:04:56 +08:00
cache_result = redis_client.get(cache_key)
if cache_result is not None:
sas_token = cache_result.decode("utf-8")
2024-06-04 13:04:56 +08:00
else:
sas_token = generate_account_sas(
account_name=self.account_name,
account_key=self.account_key,
resource_types=ResourceTypes(service=True, container=True, object=True),
permission=AccountSasPermissions(read=True, write=True, delete=True, list=True, add=True, create=True),
expiry=datetime.now(timezone.utc).replace(tzinfo=None) + timedelta(hours=1),
2024-06-04 13:04:56 +08:00
)
2024-06-04 14:30:23 +08:00
redis_client.set(cache_key, sas_token, ex=3000)
2024-06-04 13:04:56 +08:00
return BlobServiceClient(account_url=self.account_url, credential=sas_token)