Azure Storage
Introduction
Azure Storage provides a modern data storage solution for applications, offering high availability, security, durability, and scalability.
Storage Types:
- Blob Storage (object storage)
- File Storage (file shares)
- Table Storage (NoSQL)
- Queue Storage (messages)
- Disk Storage (VM disks)
Blob Storage
Container Management
# Create storage account
az storage account create \
--name mystorageaccount \
--resource-group myResourceGroup \
--location eastus \
--sku Standard_LRS
# Create container
az storage container create \
--name mycontainer \
--account-name mystorageaccount
# Upload blob
az storage blob upload \
--account-name mystorageaccount \
--container-name mycontainer \
--name myblob.txt \
--file myfile.txt
Access Tiers
Available Tiers:
- Hot - Frequent access
- Cool - Infrequent access
- Archive - Long-term storage
File Storage
File Share Operations
# Create file share
az storage share create \
--name myshare \
--account-name mystorageaccount
# Upload file
az storage file upload \
--share-name myshare \
--source myfile.txt
# Create directory
az storage directory create \
--share-name myshare \
--name mydirectory
Mounting Shares
# Linux mount command
sudo mount -t cifs \
//mystorageaccount.file.core.windows.net/myshare \
/mnt/myshare \
-o credentials=/etc/smbcredentials/mystorageaccount.cred
# Windows PowerShell
net use Z: \\mystorageaccount.file.core.windows.net\myshare
Table Storage
Table Operations
from azure.data.tables import TableServiceClient
# Create table
connection_string = "DefaultEndpointsProtocol=https;..."
table_service = TableServiceClient.from_connection_string(connection_string)
table_service.create_table("customers")
# Insert entity
from azure.data.tables import TableEntity
entity = TableEntity()
entity['PartitionKey'] = 'region1'
entity['RowKey'] = 'customer1'
entity['email'] = 'customer@example.com'
table_client.create_entity(entity=entity)
Query Data
# Query entities
query_filter = "PartitionKey eq 'region1'"
entities = table_client.query_entities(query_filter)
for entity in entities:
print(entity['RowKey'], entity['email'])
Queue Storage
Message Operations
from azure.storage.queue import QueueServiceClient
# Create queue
connection_string = "DefaultEndpointsProtocol=https;..."
queue_service = QueueServiceClient.from_connection_string(connection_string)
queue_client = queue_service.create_queue("myqueue")
# Send message
queue_client.send_message("Hello, World!")
# Receive messages
messages = queue_client.receive_messages()
Queue Features:
- Message TTL
- Visibility timeout
- Dead letter support
- Async processing
Security
Access Control
# Generate SAS token
az storage account generate-sas \
--account-name mystorageaccount \
--services bfqt \
--resource-types sco \
--permissions rwdlacup \
--expiry 2024-01-01
# Set storage account key
az storage account keys renew \
--account-name mystorageaccount \
--resource-group myResourceGroup \
--key key1
Encryption
Security Features:
- Azure AD integration
- Encryption at rest
- Encryption in transit
- Customer-managed keys
- Private endpoints
Best Practices
Performance Optimization
Tips:
- Use appropriate access tiers
- Implement retry logic
- Use CDN for frequent access
- Enable soft delete
- Monitor metrics
Cost Management
Cost Optimization:
- Choose correct storage tier
- Implement lifecycle management
- Clean up unused resources
- Use appropriate redundancy
- Monitor usage patterns