import os.path from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient from azure.core.exceptions import ResourceNotFoundError from azure.identity import DefaultAzureCredential class AzureBlobLoader: def __init__(self, account_url,bucket_name): self.bucket_name = bucket_name credential = DefaultAzureCredential() self.blob_service_client = BlobServiceClient(account_url=account_url, credential=credential) def get_file(self, filename): blob_client = self.blob_service_client.get_blob_client(container=self.bucket_name, blob=filename) return blob_client.download_blob().readall() def get_file_list(self, path): container_client = self.blob_service_client.get_container_client(self.bucket_name) return [blob.name for blob in container_client.list_blobs(name_starts_with=path)] def file_exists(self, path): container_client = self.blob_service_client.get_container_client(self.bucket_name) prefix = path return any([blob.name == path for blob in container_client.list_blobs(name_starts_with=prefix)]) def upload_file(self, filename, key): blob_client = self.blob_service_client.get_blob_client(container=self.bucket_name, blob=key) with open(filename, "rb") as data: blob_client.upload_blob(data, overwrite=True) def save_file(self, filename, content): raise Exception("azure blob loader does not support save file")