How to Use Azure Blob Storage
This guide provides detailed instructions on common operations performed with Azure Blob Storage. Azure Blob Storage is Microsoft's cloud object storage solution for the latest generation of cloud-native applications.
Tip: Before you begin, ensure you have an Azure subscription and a storage account. If you don't have one, you can create a free trial account.
1. Creating and Managing Containers
Containers are fundamental to organizing blobs. They are analogous to folders in a file system.
Creating a Container
You can create containers using the Azure portal, Azure CLI, PowerShell, or client libraries.
Using Azure CLI
az storage container create \
--name mycontainer \
--account-name mystorageaccount \
--account-key
Replace mycontainer with your desired container name and mystorageaccount with your storage account name. You'll need your storage account key.
Using Azure Portal
Navigate to your storage account in the Azure portal, then select "Containers" under "Data storage". Click "+ Container" and provide a name.
2. Uploading and Downloading Blobs
Blob storage supports three types of blobs: block blobs, append blobs, and page blobs.
Uploading a Block Blob
Block blobs are ideal for storing documents, media files, and other general-purpose data.
Using Azure PowerShell
# Connect to Azure
Connect-AzAccount
# Set your subscription context
Set-AzContext -SubscriptionId ""
# Upload a blob
$ctx = New-AzStorageContext -StorageAccountName "mystorageaccount" -StorageAccountKey ""
Set-AzStorageBlobContent -File "C:\path\to\your\local\file.txt" -Container "mycontainer" -Blob "myblob.txt" -Context $ctx
Downloading a Blob
Download blobs to your local machine or application.
Using Azure CLI
az storage blob download \
--container-name mycontainer \
--name myblob.txt \
--file C:\path\to\save\downloaded_blob.txt \
--account-name mystorageaccount \
--account-key
3. Managing Access and Permissions
Control who can access your blobs and what they can do.
Shared Access Signatures (SAS)
SAS provides a way to delegate restricted access to your blobs without sharing your account keys.
Important: Generate SAS tokens with the minimum necessary permissions and for the shortest duration required.
Access Control Lists (ACLs)
For containers, you can define public access levels: Private, Blob, or Container.
- Private: Only authenticated users with account access keys can access blobs.
- Blob: Anonymous read access to blobs within the container, but only when the blob is specified.
- Container: Anonymous read access to the container and its blobs.
4. Working with Blob Metadata and Properties
Each blob has system properties (like content type) and user-defined metadata.
Setting Metadata
You can associate custom key-value pairs with a blob.
Using Azure SDK for Python
from azure.storage.blob import BlobServiceClient
connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client("mycontainer")
blob_client = container_client.get_blob_client("myblob.txt")
metadata = {"purpose": "user-data", "batch": "batch1"}
blob_client.set_blob_metadata(metadata=metadata)
print("Metadata set successfully.")
For more advanced scenarios and detailed API references, please refer to the official Azure Blob Storage documentation.