How to Manage Azure Storage Blobs
This guide provides detailed instructions on how to manage blobs in Azure Blob Storage, covering common operations such as creating, uploading, downloading, listing, and deleting blobs, as well as managing their metadata and properties.
1. Creating and Managing Containers
Containers are logical groupings for blobs. You can create, list, and delete containers using the Azure portal, Azure CLI, Azure PowerShell, or client libraries.
Using Azure CLI:
# Create a container
az storage container create --name mycontainer --account-name mystorageaccount --auth-mode login
# List containers
az storage container list --account-name mystorageaccount --auth-mode login --output table
# Delete a container
az storage container delete --name mycontainer --account-name mystorageaccount --auth-mode login
Using Azure PowerShell:
# Create a container
New-AzStorageContainer -Name "mycontainer" -Context (Get-AzStorageAccount -ResourceGroupName "myrg" -Name "mystorageaccount").Context
# List containers
Get-AzStorageContainer -Context (Get-AzStorageAccount -ResourceGroupName "myrg" -Name "mystorageaccount").Context | Format-Table
# Delete a container
Remove-AzStorageContainer -Name "mycontainer" -Context (Get-AzStorageAccount -ResourceGroupName "myrg" -Name "mystorageaccount").Context
2. Uploading Blobs
Azure Blob Storage supports uploading various types of blobs, including block blobs, append blobs, and page blobs. Block blobs are the most common type.
Uploading a Block Blob using Azure CLI:
# Upload a file as a block blob
az storage blob upload --account-name mystorageaccount --container-name mycontainer --name myblob.txt --file /path/to/your/local/file.txt --auth-mode login
Uploading a Block Blob using Azure PowerShell:
# Get storage account context
$ctx = (Get-AzStorageAccount -ResourceGroupName "myrg" -Name "mystorageaccount").Context
# Upload a blob
Set-AzStorageBlobContent -Container "mycontainer" -File "/path/to/your/local/file.txt" -Blob "myblob.txt" -Context $ctx
3. Downloading Blobs
You can download blobs from your storage account to your local machine.
Downloading a Blob using Azure CLI:
# Download a blob
az storage blob download --account-name mystorageaccount --container-name mycontainer --name myblob.txt --file ./downloaded-myblob.txt --auth-mode login
Downloading a Blob using Azure PowerShell:
# Get storage account context
$ctx = (Get-AzStorageAccount -ResourceGroupName "myrg" -Name "mystorageaccount").Context
# Download a blob
Get-AzStorageBlobContent -Container "mycontainer" -Blob "myblob.txt" -Destination "./downloaded-myblob.txt" -Context $ctx
4. Listing Blobs
Listing blobs within a container is a fundamental operation for managing your storage.
Listing Blobs using Azure CLI:
# List all blobs in a container
az storage blob list --account-name mystorageaccount --container-name mycontainer --output table --auth-mode login
Listing Blobs using Azure PowerShell:
# Get storage account context
$ctx = (Get-AzStorageAccount -ResourceGroupName "myrg" -Name "mystorageaccount").Context
# List blobs
Get-AzStorageBlob -Container "mycontainer" -Context $ctx | Format-Table Name, Length, LastModified
5. Deleting Blobs
Blobs can be deleted individually or in bulk.
Deleting a Blob using Azure CLI:
# Delete a specific blob
az storage blob delete --account-name mystorageaccount --container-name mycontainer --name myblob.txt --auth-mode login
Deleting a Blob using Azure PowerShell:
# Get storage account context
$ctx = (Get-AzStorageAccount -ResourceGroupName "myrg" -Name "mystorageaccount").Context
# Delete a blob
Remove-AzStorageBlob -Container "mycontainer" -Blob "myblob.txt" -Context $ctx
6. Managing Blob Metadata and Properties
Blobs have system properties (like size and last modified date) and user-defined metadata that you can set and retrieve.
Setting Metadata using Azure CLI:
# Set metadata for a blob
az storage blob metadata update --account-name mystorageaccount --container-name mycontainer --name myblob.txt --metadata '{"key1":"value1", "key2":"value2"}' --auth-mode login
Getting Metadata using Azure CLI:
# Get metadata for a blob
az storage blob show --account-name mystorageaccount --container-name mycontainer --name myblob.txt --query metadata --auth-mode login
7. Blob Access Tiers
Azure Blob Storage offers different access tiers (Hot, Cool, Archive) to optimize costs based on access frequency.
- Hot: For frequently accessed data.
- Cool: For infrequently accessed data stored for at least 30 days.
- Archive: For rarely accessed data stored for at least 180 days.
You can change the access tier of a blob or an entire container.
Changing Access Tier using Azure CLI:
# Change blob access tier to Cool
az storage blob update --account-name mystorageaccount --container-name mycontainer --name myblob.txt --tier Cool --auth-mode login
Conclusion
Managing Azure Storage blobs is a straightforward process with the help of Azure tools and SDKs. By understanding these core operations, you can efficiently store, organize, and access your data in the cloud.