Azure Blob Storage is a Microsoft-managed cloud service for storing massive amounts of unstructured data such as text and binary data. If you're working on a project that involves file uploads, integrating with Azure Blob Storage can be an efficient and scalable solution.This post provides python code example for uploading files to Azure Blob Storage using Python and the Azure SDK.
PrerequisitesFile Uploading To Azure Blob Storage Using Python
Getting Started
Azure Storage provides scalable, durable cloud storage for data objects, and it's a popular choice for applications that require persistent file storage. Whether you're building a web app, handling backups, or managing large datasets, knowing how to upload files to Azure Blob Storage using Python is a key skill.
- An Azure Account: You need an active Azure subscription.
- Azure Storage Account: Create one in the Azure Portal if not created.
- Container: Create a container inside your storage account if not available.
To upload files to Azure Blob Storage using Python, you install the azure-storage-blob
package (client library). Copy the below command and execute in command prompt.
pip install azure-storage-blob
Uploading Files Azure Storage
Here are the python code examples for uploading file to Azure blog storage using pythong
Upload a File from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import os
# Replace with your actual connection string and container name
CONNECTION_STRING = "DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...;EndpointSuffix=core.windows.net"
CONTAINER_NAME = "your-container-name"
FILE_PATH = "path/to/your/file.txt" # Local file to upload
BLOB_NAME = "uploaded-file.txt" # Name in Azure
# Initialize the BlobServiceClient
blobServiceClient = BlobServiceClient.from_connection_string(CONNECTION_STRING)
# Get the container client
azureContainer = blobServiceClient.get_container_client(CONTAINER_NAME)
try:
blobServiceClient.create_container()
print(f"Container '{CONTAINER_NAME}' created.")
except Exception:
print(f"Container '{CONTAINER_NAME}' may already exist.")
# Upload the file
with open(FILE_PATH, "rb") as data:
azureBlob = azureContainer.get_blob_client(BLOB_NAME)
azureBlob.upload_blob(data, overwrite=True)
print(f"'{FILE_PATH}' uploaded to blob '{BLOB_NAME}' in container '{CONTAINER_NAME}'")
Upload Multiple Files
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import os
# Replace with your actual connection string and container name
CONNECTION_STRING = "DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...;EndpointSuffix=core.windows.net"
CONTAINER_NAME = "your-container-name"
FILE_PATH = "./MyFiles" # Local file to upload
BLOB_NAME = "uploaded-file.txt" # Name in Azure
# Initialize the BlobServiceClient
blobServiceClient = BlobServiceClient.from_connection_string(CONNECTION_STRING)
# Get the container client
azureContainer = blobServiceClient.get_container_client(CONTAINER_NAME)
try:
blobServiceClient.create_container()
print(f"Container '{CONTAINER_NAME}' created.")
except Exception:
print(f"Container '{CONTAINER_NAME}' may already exist.")
# Upload the files
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
if os.path.isfile(file_path):
with open(file_path, "rb") as data:
azureBlob = azureContainer.get_blob_client(filename)
azureBlob.upload_blob(data, overwrite=True)
print(f"Uploaded: {filename}")
Upload a Folder and it's Content
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import os
# Replace with your actual connection string and container name
CONNECTION_STRING = "DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...;EndpointSuffix=core.windows.net"
CONTAINER_NAME = "your-container-name"
FILE_PATH = "./MyFiles" # Local file to upload
BLOB_NAME = "uploaded-file.txt" # Name in Azure
# Initialize the BlobServiceClient
blobServiceClient = BlobServiceClient.from_connection_string(CONNECTION_STRING)
# Get the container client
azureContainer = blobServiceClient.get_container_client(CONTAINER_NAME)
try:
blobServiceClient.create_container()
print(f"Container '{CONTAINER_NAME}' created.")
except Exception:
print(f"Container '{CONTAINER_NAME}' may already exist.")
# Upload folder content recursively
for root, dirs, files in os.walk(local_folder_path):
for filename in files:
file_path_on_disk = os.path.join(root, filename)
relative_path = os.path.relpath(file_path_on_disk, local_folder_path)
blob_path = relative_path.replace("\\", "/") # Use forward slashes for blob paths
print(f"Uploading {file_path_on_disk} to {blob_path}...")
azureBlob = blobServiceClient.get_blob_client(container=container_name, blob=blob_path)
with open(file_path_on_disk, "rb") as data:
azureBlob.upload_blob(data, overwrite=True)
print("Upload complete.")
Note:-Azure SDK for python does not support directly upload a folder and its content. you have to recursively upload the folder content.
Summary
Uploading files to Azure Blob Storage using Python is straightforward with the Azure SDK. Once configured, it becomes a powerful way to manage files in the cloud programmatically. This script can be integrated into larger systems such as web apps, data pipelines, or automation tools I hope this was helpful to you.
Thanks