Stage 7 · Master
Cloud SDKs & Azure Automation
Azure Storage SDK
Upload/download blobs, manage file shares, and table storage.
Azure Storage Overview
| Service | Type | Use Case |
|---|---|---|
| Blob Storage | Unstructured | Logs, backups, images, artifacts |
| File Share | SMB/NFS files | Shared config files, legacy app data |
| Table Storage | NoSQL key-value | Lightweight metadata, logs |
| Queue Storage | Messages | Decoupling services, async processing |
# Blob storage
pip install azure-storage-blob
# File shares
pip install azure-storage-file-share
# Table storage
pip install azure-data-tables
# All-in-one (optional)
pip install azure-storage-blob azure-storage-file-share azure-data-tablesEach storage service has its own package. Install only what you need. The blob package is the most commonly used for DevOps scripts.
Blob Operations
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, BlobType
from pathlib import Path
credential = DefaultAzureCredential()
blob_service = BlobServiceClient(
account_url="https://mystorage.blob.core.windows.net/",
credential=credential,
)
# Upload a file
container = blob_service.get_container_client("backups")
blob = container.get_blob_client("2024/01/backup.sql")
blob.upload_blob(
data=Path("backup.sql").read_bytes(),
blob_type=BlobType.BLOCKBLOB,
overwrite=True,
)
print(f"Uploaded: {blob.blob_name}")
# Download a blob
downloaded = blob.download_blob()
content = downloaded.readall()
Path("downloaded-backup.sql").write_bytes(content)
# List blobs in a container
for blob in container.list_blobs(name_starts_with="2024/"):
print(f"{blob.name}: {blob.size} bytes, last modified {blob.last_modified}")BlobServiceClient is the entry point. Get a container client for a specific container, then a blob client for a specific blob. upload_blob with overwrite=True replaces existing blobs.
File Shares
from azure.identity import DefaultAzureCredential
from azure.storage.fileshare import ShareServiceClient
credential = DefaultAzureCredential()
share_service = ShareServiceClient(
account_url="https://mystorage.file.core.windows.net/",
credential=credential,
)
# Create a share
share = share_service.create_share("config-files")
print(f"Created share: {share.name}")
# Upload a file
share_client = share_service.get_share_client("config-files")
file_client = share_client.get_file_client("app/config.yaml")
file_client.upload_file(Path("config.yaml").read_bytes())
# List files
for item in share_client.list_directories_and_files():
print(f"{'DIR' if item.is_directory else 'FILE'}: {item.name}")
# Download a file
downloaded = file_client.download_file()
content = downloaded.readall()File shares provide SMB access from Azure VMs and on-premises. Use them for shared configuration files or legacy applications that require file system access.
Table Storage
from azure.identity import DefaultAzureCredential
from azure.data.tables import TableServiceClient, TableEntity
credential = DefaultAzureCredential()
table_service = TableServiceClient(
account_url="https://mystorage.table.core.windows.net/",
credential=credential,
)
# Create a table
table = table_service.create_table("metrics")
# Insert an entity
table_client = table_service.get_table_client("metrics")
entity: TableEntity = {
"PartitionKey": "web-01",
"RowKey": "2024-01-15T14:00:00Z",
"cpu_percent": 45.2,
"memory_percent": 62.1,
"disk_io": 1024,
}
table_client.create_entity(entity)
# Query entities
for e in table_client.query_entities(
filter="PartitionKey eq 'web-01'",
):
print(f"{e['RowKey']}: CPU={e['cpu_percent']}%")
# Update an entity
entity["cpu_percent"] = 78.5
table_client.update_entity(entity)Table Storage is a NoSQL key-value store. PartitionKey groups related entities. RowKey uniquely identifies each entity within a partition. Together they form the primary key.
SAS Tokens
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient
from datetime import datetime, timedelta
credential = DefaultAzureCredential()
blob_service = BlobServiceClient(
account_url="https://mystorage.blob.core.windows.net/",
credential=credential,
)
# Generate a SAS token for time-limited read access
from azure.storage.blob import generate_blob_sas, BlobSasPermissions
sas_token = generate_blob_sas(
account_name="mystorage",
container_name="backups",
blob_name="backup.sql",
account_key=None, # Use account key or AAD credential
user_delegation_key=blob_service.get_user_delegation_key(
key_start_time=datetime.utcnow(),
key_expiry_time=datetime.utcnow() + timedelta(hours=1),
),
permission=BlobSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1),
)
url = f"https://mystorage.blob.core.windows.net/backups/backup.sql?{sas_token}"
print(f"Temporary URL: {url}")SAS tokens grant time-limited access to specific blobs or containers. Use them for temporary access without sharing credentials. They expire automatically.
Storage Monitoring
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient
def get_container_stats(account_url: str) -> list[dict]:
"""Get statistics for all containers in a storage account."""
credential = DefaultAzureCredential()
blob_service = BlobServiceClient(account_url=account_url, credential=credential)
stats = []
for container in blob_service.list_containers():
client = blob_service.get_container_client(container.name)
total_size = 0
blob_count = 0
for blob in client.list_blobs():
total_size += blob.size
blob_count += 1
stats.append({
"name": container.name,
"blob_count": blob_count,
"total_size_mb": round(total_size / (1024 * 1024), 2),
})
return sorted(stats, key=lambda x: x["total_size_mb"], reverse=True)
# Usage
for stat in get_container_stats("https://mystorage.blob.core.windows.net/"):
print(f"{stat['name']}: {stat['blob_count']} blobs, {stat['total_size_mb']} MB")Listing all blobs and summing their sizes gives you accurate storage usage. For large accounts, this can be slow — use Azure Monitor metrics for real-time capacity data.
Azure Blob Storage supports lifecycle policies that automatically move blobs to cooler tiers or delete them after a set period. Configure this in the Azure portal or via SDK to reduce costs.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.