Audvik Labs

Installing Azure blob using Python

How to upload files to Azure blob using Python

Azure Blob storage is Microsoft’s object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data, such as text or binary data.

Blob storage is ideal for:

  • Serving images or documents directly to a browser
  • Storing files for distributed access

  • Streaming video and audio

  • Storing data for backup and restore, disaster recovery, and archiving

  • Storing data for analysis by an on-premises or Azure-hosted service


Python 2.7, or 3.5 or later is required to use this package.

You must have an Azure subscription and an Azure storage account to use this package.

Install the package

Install the Azure Storage Blobs client library for Python with pip:

pip install azure-storage-blob

Create a storage account

If you wish to create a new storage account, you can use the Azure Portal, Azure PowerShell.

Create the client

The Azure Storage Blobs client library for Python allows you to interact with three types of resources: the storage account itself, blob storage containers, and blobs. Interaction with these resources starts with an instance of a client. To create a client object, you will need the storage account’s blob service account URL and a credential that allows you to access the storage account:

from import BlobServiceClient
service = BlobServiceClient(account_url="", credential=credential)

Looking up the account URL

You can find the storage account’s blob service URL using the Azure Portal, Azure PowerShell.

Key Points

  • Create a Storage Account using the Azure Portal.
  • Create a container.
  • Upload a file to block blob.
  • List blobs.
  • Download a blob to file.
  • Delete a blob.
  • Delete the container.
  • How to Upload Files to Azure Storage Blobs Using Python


The following program demonstrates a typical use case where you want to bulk upload a set of jpg images from a local folder to the Azure blob storage container. Note that for large number of files, this program may not be efficient as it sequentially uploads the images. 

Replace MY_CONNECTION_STRING, MY_IMAGE_CONTAINER and LOCAL_IMAGE_PATH before running the program :

# Python program to bulk upload jpg image files as blobs to azure storage
# Uses latest python SDK() for Azure blob storage
# Requires python 3.6 or above
import os
from import BlobServiceClient  BlobClient
from import ontentSettings, ContainerClient
# IMPORTANT: Replace connection string with your storage account connection string
# Usually starts with DefaultEndpointsProtocol= https;...
# Replace with blob container. This should be already created in azure storage.
# Replace with the local folder which contains the image files for upload
class AzureBlobFileUploader:
def __init__(self):
print("Intializing AzureBlobFileUploader")
# Initialize the connection to Azure storage account
self.blob_service_client =  BlobServiceClient.from_connection_string(MY_CONNECTION_STRING)
def upload_all_images_in_folder(self): 
# Get all files with jpg extension and exclude directories
all_file_names = [f for f in os.listdir(LOCAL_IMAGE_PATH)
if os.path.isfile(os.path.join(LOCAL_IMAGE_PATH, f)) and ".jpg" in f]
# Upload each file
for file_name in all_file_names:
self.upload_image(file_name) def upload_image(self,file_name): 
# Create blob with same name as local file name 
blob_client = self.blob_service_client.get_blob_client(container = MY_IMAGE_CONTAINER, blob = file_name)
# Get full path to the file
upload_file_path = os.path.join(LOCAL_IMAGE_PATH, file_name)
# Create blob on storage
# Overwrite if it already exists.

Leave a comment

Your email address will not be published. Required fields are marked *