This module provides different methods to access Azure Blob Storage using various authentication approaches.
- Multiple authentication methods:
- Connection String
- SAS Token
- Microsoft Entra ID (formerly Azure AD)
- Blob download functionality
- Error handling and validation
- Python 3.x
- Azure Storage Account
- Azure AD App Registration (for Entra ID authentication)
- Clone the repository
- Install required packages:
pip install azure-storage-blob azure-identity python-dotenv
- Create a
.env
file with the required environment variables. You can use the.env.template
file as a reference.
- Copy
.env.template
to.env
:
cp .env.template .env
- Fill in your Azure credentials in
.env
:
AZURE_STORAGE_URL=https://<your-storage-account>.blob.core.windows.net/
AZURE_TENANT_ID=<your-tenant-id>
AZURE_CLIENT_ID=<your-client-id>
AZURE_CLIENT_SECRET=<your-client-secret>
AZURE_CONNECTION_STRING=<your-connection-string>
AZURE_SAS_TOKEN=<your-sas-token>
- Update
config.py
with your container name:
CONTAINER_NAME = "your-container-name"
from main import EntraIDBlobStorage
# Initialize storage client
blob_storage = EntraIDBlobStorage()
# Download blob
data = blob_storage.download_blob("your-blob-name.txt")
print(data)
from main import SASBlobStorage
blob_storage = SASBlobStorage()
data = blob_storage.download_blob("your-blob-name.txt")
from main import ConStrBlobStorage
blob_storage = ConStrBlobStorage()
data = blob_storage.download_blob("your-blob-name.txt")
- Use Microsoft Entra ID authentication when possible
- Rotate credentials regularly
- Use minimum required permissions
- Store sensitive credentials in Azure Key Vault in production
We can use the same service principal (same client ID, client secret, and tenant ID) for accessing multiple Azure Servies. The differences lies in teh permission/policies you grant to that service principal
The best practces are:
- Create one service principal for your application
- Grant the minimum required permissions for each sergie it needs to access
- Use teh same credentials to authenticate, but different services will check their own access policies.
For Microsoft Entra ID authentication, ensure your service principal has:
Storage Blob Data Reader
role (minimum for reading blobs)Reader
role (for container operations)Key Vault Administrator
role (for accessing Key Vault)