Azure Blob Storage Operations
This document details the common operations you can perform on Azure Blob Storage, including creating, retrieving, updating, and deleting blobs and containers. Understanding these operations is fundamental to effectively managing your unstructured data in the cloud.
Important Note
Operations are typically performed using the Azure Storage REST API, Azure SDKs (available for various languages like .NET, Java, Python, Node.js, Go, and C++), Azure CLI, or Azure PowerShell.
Container Operations
Creating a Container
Before you can store blobs, you need to create a container. Containers are logical groupings of blobs.
# Example using Azure CLI to create a container
az storage container create --name mycontainer --account-name mystorageaccount --auth-mode login
Listing Containers
Retrieve a list of all containers within a storage account.
# Example using Azure PowerShell to list containers
Get-AzStorageContainer -Context $ctx
Getting Container Properties
Retrieve metadata and properties of a specific container.
# Example using Python SDK
from azure.storage.blob import ContainerClient
container_client = ContainerClient.from_connection_string(conn_str, container_name="mycontainer")
properties = container_client.get_container_properties()
print(properties)
Deleting a Container
Remove a container and all of its blobs. This operation is irreversible.
# Example using Azure CLI to delete a container
az storage container delete --name mycontainer --account-name mystorageaccount --auth-mode login
Blob Operations
Uploading Blobs
Upload data to a blob. Azure Blob Storage supports three types of blobs:
- Block Blobs: Optimized for storing large amounts of unstructured data, such as text or binary data.
- Append Blobs: Optimized for append operations, such as logging data.
- Page Blobs: Optimized for random read/write operations.
// Example using .NET SDK to upload a block blob
using Azure.Storage.Blobs;
var blobClient = new BlobClient(connectionString, "mycontainer", "myblob.txt");
using (var fileStream = File.OpenRead("local_file.txt"))
{
blobClient.Upload(fileStream);
}
Downloading Blobs
Retrieve the contents of a blob.
// Example using Java SDK to download a blob
BlobClient blobClient = storageClient.getBlobClient("mycontainer", "myblob.txt");
blobClient.downloadToFile("downloaded_file.txt", true);
Listing Blobs
List blobs within a container. You can specify prefixes to filter results.
# Example using Azure CLI to list blobs
az storage blob list --container-name mycontainer --account-name mystorageaccount --prefix "data/"
Getting Blob Properties
Retrieve metadata and properties of a specific blob, such as its size, last modified time, and content type.
# Example using Python SDK
from azure.storage.blob import BlobClient
blob_client = BlobClient.from_connection_string(conn_str, container_name="mycontainer", blob_name="myblob.txt")
properties = blob_client.get_blob_properties()
print(properties)
Deleting Blobs
Remove a blob from its container.
# Example using Azure PowerShell to delete a blob
Remove-AzStorageBlob -Container mycontainer -Blob myblob.txt -Context $ctx
Blob Metadata and Properties
You can associate custom metadata with a blob or container, which is stored as key-value pairs. Azure Storage also automatically manages various properties for each blob, such as Content-Type, Content-Encoding, and Last-Modified.
Tip
Metadata is useful for storing application-specific information about your blobs. It is accessible alongside the blob data without incurring additional retrieval costs for the metadata itself.
Common Use Cases
- Storing and serving static website content.
- Archiving backups and logs.
- Distributing large files.
- Serving images, videos, and audio.
- Data lakes for big data analytics.
For detailed API references and SDK examples, please refer to the REST API documentation and the respective SDK guides.