File Downloading From Azure Blob Storage In Python

Azure Blob Storage is Microsoft's object storage solution for the cloud, optimized for storing massive amounts of unstructured data. In this post, we’ll explore how to download files (blobs) from Azure Blob Storage using Python.

File Downloading From Azure Blob Storage in Python

Getting Started

Azure Blob Storage is a scalable, cost-effective service for storing unstructured data in the cloud—like documents, images, and backups. Often, developers need to programmatically download files from a Blob container using Python.

Prerequisites

Before diving into the code, ensure you have the following:
  • An Azure subscription
  • A storage account and a container with at least one file (blob)
  • Python installed (Python 3.6 or newer)
  • Required Python packages installed

Install Required Package

The official SDK for interacting with Azure Blob Storage in Python is azure-storage-blob. Install it using pip:
 pip install azure-storage-blob  

File Downloading From Azure Blob Storage

You’ll need to connect to Azure Blob Storage using your connection string for this demonstrarion. there is a another approch also there to downlod file from blob storage using credentials from Azure AD but here we will download files using connection string.

Single File Download
 from azure.storage.blob import BlobServiceClient  
 from azure.storage.blob import BlobClient  
 # Replace with your actual connection string  
 connection_string = "DefaultEndpointsProtocol=https;AccountName=your_account_name;AccountKey=your_account_key;EndpointSuffix=core.windows.net"  
 blob_service_client = BlobServiceClient.from_connection_string(connection_string)  
 # Parameters  
 container_name = "your-container-name"  
 blob_name = "example.txt"  
 download_file_path = "downloaded_example.txt"  
 # Create blob client  
 blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)  
 # Download the blob to a local file  
 with open(download_file_path, "wb") as download_file:  
   download_data = blob_client.download_blob()  
   download_file.write(download_data.readall())  
 print(f"Blob downloaded to: {download_file_path}")  

Multiple File Download
 from azure.storage.blob import ContainerClient  
 from azure.storage.blob import BlobServiceClient  
 import os  
 # Replace with your actual connection string  
 connection_string = "DefaultEndpointsProtocol=https;AccountName=your_account_name;AccountKey=your_account_key;EndpointSuffix=core.windows.net"  
 blob_service_client = BlobServiceClient.from_connection_string(connection_string)  
 # Initialize container client  
 container_client = blob_service_client.get_container_client(container_name)  
 # Create local folder to store downloads  
 os.makedirs("downloads", exist_ok=True)  
 # Iterate and download all blobs  
 for blob in container_client.list_blobs():  
   blob_client = container_client.get_blob_client(blob)  
   download_path = os.path.join("downloads", blob.name)  
   with open(download_path, "wb") as file:  
     blob_data = blob_client.download_blob()  
     file.write(blob_data.readall())  
   print(f"Downloaded: {blob.name}")  

Summary

Downloading files from Azure Blob Storage using Python is easy with the azure-storage-blob SDK. Whether you’re working with connection strings or SAS tokens, the SDK provides powerful and secure tools for managing blob data.

Thanks

Kailash Chandra Behera

An IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

نموذج الاتصال