How to Use Azure Blob Storage with Python: Simple Guide
To use
Azure Blob Storage with Python, install the azure-storage-blob package, create a BlobServiceClient with your connection string, and then use it to upload or download blobs. This lets you store and retrieve files in the cloud easily from your Python code.Syntax
Here is the basic syntax to connect and upload a file to Azure Blob Storage using Python:
BlobServiceClient: Connects to your storage account.ContainerClient: Accesses a specific container (folder).upload_blob(): Uploads data as a blob (file).
python
from azure.storage.blob import BlobServiceClient # Connect to the storage account blob_service_client = BlobServiceClient.from_connection_string('<your_connection_string>') # Get container client container_client = blob_service_client.get_container_client('<container_name>') # Upload a blob with open('example.txt', 'rb') as data: container_client.upload_blob(name='example.txt', data=data, overwrite=True)
Example
This example shows how to upload a text file to Azure Blob Storage and then download it back to verify the content.
python
from azure.storage.blob import BlobServiceClient connection_string = '<your_connection_string>' container_name = 'mycontainer' blob_name = 'sample.txt' local_file = 'sample.txt' def upload_blob(): blob_service_client = BlobServiceClient.from_connection_string(connection_string) container_client = blob_service_client.get_container_client(container_name) try: container_client.create_container() except Exception: pass # Container may already exist with open(local_file, 'rb') as data: container_client.upload_blob(name=blob_name, data=data, overwrite=True) print(f'Uploaded {blob_name} to container {container_name}') def download_blob(): blob_service_client = BlobServiceClient.from_connection_string(connection_string) container_client = blob_service_client.get_container_client(container_name) blob_client = container_client.get_blob_client(blob_name) download_stream = blob_client.download_blob() content = download_stream.readall().decode('utf-8') print(f'Downloaded blob content:\n{content}') if __name__ == "__main__": upload_blob() download_blob()
Output
Uploaded sample.txt to container mycontainer
Downloaded blob content:
Hello Azure Blob Storage!
Common Pitfalls
- Not installing the
azure-storage-blobpackage before running code. - Using an incorrect or expired connection string.
- Trying to upload to a container that does not exist without creating it first.
- Not setting
overwrite=Truewhen uploading blobs to replace existing files. - Forgetting to open files in binary mode (
'rb') when uploading.
python
from azure.storage.blob import BlobServiceClient # Wrong: Not opening file in binary mode with open('file.txt', 'r') as data: container_client.upload_blob(name='file.txt', data=data) # This will fail # Right: Open file in binary mode with open('file.txt', 'rb') as data: container_client.upload_blob(name='file.txt', data=data, overwrite=True)
Quick Reference
Keep these tips handy when working with Azure Blob Storage in Python:
- Install package:
pip install azure-storage-blob - Use
BlobServiceClient.from_connection_string()to connect. - Create container if missing:
container_client.create_container(). - Upload blobs with
upload_blob(name, data, overwrite=True). - Download blobs with
download_blob().readall().
Key Takeaways
Install and import the azure-storage-blob package to work with Blob Storage in Python.
Use BlobServiceClient with your connection string to connect to your storage account.
Always open files in binary mode when uploading blobs to avoid errors.
Create containers before uploading blobs if they do not exist.
Set overwrite=True to replace existing blobs when uploading.