I'm jus lookin for some help. I'm very newed on python but i try to do something. I need to download a specific blob (actually is a .xlsx file) from multiples containers. I mean, the process create a container dayly but what interests me is to download a single file from each container and I've tried the following:
# download_blobs.py
# Python program to bulk download blob files from azure storage
# Uses latest python SDK() for Azure blob storage
# Requires python 3.6 or above
import os
from azure.storage.blob import BlobServiceClient, BlobClient
from azure.storage.blob import ContentSettings, ContainerClient
# IMPORTANT: Replace connection string with your storage account connection string
# Usually starts with DefaultEndpointsProtocol=https;...
MY_CONNECTION_STRING = "my_conection_string"
# Replace with blob container
MY_BLOB_CONTAINER = "^092022"
# Replace with the local folder where you want files to be downloaded
LOCAL_BLOB_PATH = "a_local_path"
# Replace with the blob to download
BLOB_NAME = "^xlsx'"
class AzureBlobFileDownloader:
def __init__(self):
print("Intializing AzureBlobFileDownloader")
# Initialize the connection to Azure storage account
self.blob_service_client = BlobServiceClient.from_connection_string(MY_CONNECTION_STRING)
self.my_container = self.blob_service_client.get_container_client(MY_BLOB_CONTAINER)
def save_blob(self,file_name,file_content):
# Get full path to the file
download_file_path = os.path.join(LOCAL_BLOB_PATH, file_name)
# for nested blobs, create local path as well!
os.makedirs(os.path.dirname(download_file_path), exist_ok=True)
with open(download_file_path, "wb") as file:
file.write(file_content)
def download_all_blobs_in_container(self):
my_blobs = self.my_container.list_blobs(BLOB_NAME)
for blob in my_blobs:
print(blob.name)
bytes = self.my_container.get_blob_client(blob).download_blob().readall()
self.save_blob(blob.name, bytes)
# Initialize class and upload files
azure_blob_file_downloader = AzureBlobFileDownloader()
azure_blob_file_downloader.download_all_blobs_in_container()
Each container created dayly has te following name:
01092022 - 02092022 - 03092022 - . . .
and the blob that I want to download is:
p.01092022.xlsx - p.02092022.xlsx - p.03092022.xlsx - . . .
How can I make to python go through each container and download the file related to each one according to the sequence of names they have?
Thanks for you help!
Greatings.