Azure Blob Storage is a powerful and scalable cloud object storage solution provided by Microsoft Azure. It's commonly used to store unstructured data like images, videos, documents, and backups. In this article, you'll learn how to download an image file from Azure Blob Storage using Python.
Image Download From Azure Blobe Storage using Python
Getting Started
Azure Blob Storage is a powerful cloud-based solution from Microsoft Azure that allows you to store large amounts of unstructured data like text, images, and videos. In many real-world applications, you may need to download images from Azure Blob Storage for processing, backup, or analysis.
With just a few lines of Python code, you can automate the process of downloading images from Azure Blob Storage.
Prerequisites
Before you begin, make sure you have:- An Azure Storage Account with Blob Storage enabled.
- A container that holds your image blob.
- Installed the Azure Storage Blob SDK for Python.
Image Download From Azure Blob Storage
Below two python code examples defines single image download and multple image download from Azure Blob Storage
Single Image Download
The below code example defines how to download a single image from blob store with image name
Image Downlaod Code from azure.storage.blob import BlobServiceClient
# Configuration
connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
container_name = "your-container-name"
blob_name = "image-name.jpg"
download_file_path = "downloaded-image.jpg"
# Connect to the service
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
# Download the blob
with open(download_file_path, "wb") as file:
download_stream = blob_client.download_blob()
file.write(download_stream.readall())
The above python code devided in to three blocks. The first block is the configuration where connection string , container name and the image file name have to define.
You’ll need the connection string from your Azure Storage account. You can find it in the Azure portal under:
Storage Account > Access Keys > Connection string
The second block connect to service is creating object of BlobClient
which allows you to interact with individual blobs in a container.
The rest python code uses the download_blob() method to download the image content and write it to a local file.
Multple Image Download
This code example downloads all available images from a container
Image Download code from azure.storage.blob import BlobServiceClient
import os
# Define your connection string and container name
connection_string = "<your_connection_string>"
container_name = "<your_container_name>"
# Directory where the images will be downloaded
download_folder = "downloaded_images"
os.makedirs(download_folder, exist_ok=True)
# Create the BlobServiceClient
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(container_name)
# List and download blobs
print("Downloading images from Azure Blob Storage...")
for blob in container_client.list_blobs():
blob_name = blob.name
if blob_name.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
download_file_path = os.path.join(download_folder, os.path.basename(blob_name))
with open(download_file_path, "wb") as f:
blob_data = container_client.download_blob(blob_name)
f.write(blob_data.readall())
print(f"Downloaded: {blob_name}")
print("✅ All images downloaded successfully.")
Summary
Downloading images or any blobs from Azure Blob Storage using Python is straightforward with the Azure SDK. This method is especially useful for building image processing pipelines, integrating with ML models, or simply backing up your assets.
Thanks