Manage Azure Storage Blobs

Overview

Managing Azure Blob Storage involves a variety of operations, from basic uploading and downloading to more advanced tasks like managing blob properties, creating snapshots, and configuring leases. This guide covers the common methods and tools for managing your blobs.

Using the Azure Portal

The Azure Portal provides a user-friendly graphical interface for managing your storage accounts and blobs.

  1. Navigate to your storage account in the Azure Portal.
  2. Under "Data storage," select "Containers."
  3. Click on a container to view its blobs.
  4. From here, you can upload, download, delete, move, and rename blobs. You can also manage access tiers and view blob properties.

The portal is ideal for quick operations and for users who prefer a visual approach.

Using the Azure CLI

The Azure Command-Line Interface (CLI) is a powerful tool for scripting and automating storage management tasks.

First, ensure you have the Azure CLI installed and logged in:

az login

Here are some common Azure CLI commands for managing blobs:

  • List blobs in a container:
    az storage blob list --container-name <container-name> --account-name <storage-account-name>
  • Upload a blob:
    az storage blob upload --container-name <container-name> --name <blob-name> --file <local-file-path> --account-name <storage-account-name>
  • Download a blob:
    az storage blob download --container-name <container-name> --name <blob-name> --file <local-destination-path> --account-name <storage-account-name>
  • Delete a blob:
    az storage blob delete --container-name <container-name> --name <blob-name> --account-name <storage-account-name>

For a comprehensive list of commands, refer to the Azure CLI storage blob documentation.

Using Azure PowerShell

Azure PowerShell provides another robust scripting environment for managing Azure resources, including Blob Storage.

First, ensure you have the Azure PowerShell module installed and are connected:

Connect-AzAccount

Here are some common Azure PowerShell cmdlets for managing blobs:

  • Get blob properties:
    Get-AzStorageBlob -Container <container-name> -Blob <blob-name> -Context (Get-AzStorageAccount -ResourceGroupName <resource-group-name> -Name <storage-account-name>).Context
  • Upload a blob:
    Set-AzStorageBlobContent -Container <container-name> -File <local-file-path> -Blob <blob-name> -Context (Get-AzStorageAccount -ResourceGroupName <resource-group-name> -Name <storage-account-name>).Context
  • Download a blob:
    Get-AzStorageBlobContent -Container <container-name> -Blob <blob-name> -Destination <local-destination-path> -Context (Get-AzStorageAccount -ResourceGroupName <resource-group-name> -Name <storage-account-name>).Context
  • Remove a blob:
    Remove-AzStorageBlob -Container <container-name> -Blob <blob-name> -Context (Get-AzStorageAccount -ResourceGroupName <resource-group-name> -Name <storage-account-name>).Context

Refer to the Azure PowerShell storage blob documentation for more details.

Using the Azure SDKs

For application development, you can use the Azure SDKs available for various programming languages (e.g., Python, .NET, Java, Node.js) to programmatically manage your blobs.

Here's a conceptual example using Python:


from azure.storage.blob import BlobServiceClient

connection_string = "<YOUR_CONNECTION_STRING>"
container_name = "mycontainer"
blob_name = "myblob.txt"
local_file_name = "local_file.txt"

# Create the BlobServiceClient object
blob_service_client = BlobServiceClient.from_connection_string(connection_string)

# Upload a blob
with open(local_file_name, "rb") as data:
    blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
    blob_client.upload_blob(data)
    print(f"Uploaded {blob_name}")

# Download a blob
with open("downloaded_blob.txt", "wb") as download_file:
    blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
    download_file.write(blob_client.download_blob().readall())
    print(f"Downloaded {blob_name}")
                        

You can find specific SDK examples in the Azure Storage SDK examples documentation.

Managing Blob Properties

Blobs have properties that can be set and modified, including:

  • Content-Type: Indicates the MIME type of the blob.
  • Content-Encoding: Specifies the encoding used.
  • Metadata: Key-value pairs for custom data.
  • Access Tier: Hot, Cool, or Archive for cost optimization.

These properties can be managed through the portal, CLI, PowerShell, or SDKs. For example, using the Azure CLI to set metadata:

az storage blob metadata update --container-name <container-name> --name <blob-name> --metadata key1=value1 key2=value2 --account-name <storage-account-name>

Blob Snapshots

A blob snapshot is a read-only version of a blob that is taken at a specific point in time. Snapshots are useful for backup and restore scenarios.

Note: Snapshots are billed separately from their parent blob. Deleted blobs can be restored from snapshots if versioning is enabled.

Creating a snapshot using Azure CLI:

az storage blob snapshot --container-name <container-name> --name <blob-name> --account-name <storage-account-name>

You can then access the snapshot by appending the snapshot's unique identifier (returned by the creation command) to the blob URI.

Blob Leases

A blob lease is a lock that can be placed on a blob for a specified period. Leases provide exclusive write access to a blob, preventing other clients from modifying or deleting it while the lease is active.

  • Lease Duration: Can be finite (15-60 seconds) or infinite.
  • Acquiring a Lease: You request a lease with a specified duration.
  • Renewing a Lease: Keep the lease active.
  • Breaking a Lease: Release the lease early.
  • Releasing a Lease: Explicitly end the lease.

Using Azure CLI to acquire a lease:

az storage blob lease acquire --container-name <container-name> --blob <blob-name> --lease-duration 60 --account-name <storage-account-name>

Tip: Leases are crucial for scenarios where multiple applications might attempt to modify the same blob concurrently, ensuring data integrity.