This document explains how to list containers in an Azure Storage account using various methods.
Note: Container names are case-insensitive and must start with a letter or number, and can contain only letters, numbers, and the hyphen (-) character. The hyphen cannot be the first or last character of the name.
Introduction
Containers are fundamental organizational units within Azure Blob Storage. They act as logical groupings for your blobs. You can list containers to view their properties, manage them, or retrieve data from them.
Methods for Listing Containers
You can list containers using the following methods:
Azure CLI
Use the Azure CLI to list containers in a storage account.
Command
az storage container list --account-name --output table
Explanation
az storage container list: The command to list blob containers.
--account-name <YOUR_STORAGE_ACCOUNT_NAME>: Specifies the name of your Azure Storage account.
--output table: Formats the output as a table for better readability. Other options include json, tsv, and yaml.
Get-AzStorageContainer: The cmdlet to retrieve storage containers.
-Context (New-AzStorageContext ...): Specifies the storage account context using its name and key. Alternatively, you can use -StorageAccountName and -StorageAccountKey directly.
-Table: Formats the output as a table.
Example Output
Name PublicAccess LeaseState LeaseStatus HasImmutabilityPolicy HasLegalHold
---- ------------ ---------- ----------- --------------------- ------------
my-container-1 Off Available Unlocked False False
data-archive Off Available Unlocked False False
SDK (C#)
Use the Azure Storage SDK for .NET to list containers.
Code Example
using Azure.Storage.Blobs;
using System;
// Replace with your connection string or account name/key
string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
Console.WriteLine("Containers:");
await foreach (BlobContainerItem container in blobServiceClient.GetBlobContainersAsync())
{
Console.WriteLine($"- {container.Name}");
}
Explanation
BlobServiceClient: The main client for interacting with Blob Storage.
GetBlobContainersAsync(): Asynchronously enumerates containers in the storage account.
SDK (Python)
Use the Azure Storage SDK for Python to list containers.
Code Example
from azure.storage.blob import BlobServiceClient
# Replace with your connection string or account name/key
connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
print("Containers:")
for container in blob_service_client.list_containers():
print(f"- {container['name']}")
Explanation
BlobServiceClient: The main client for interacting with Blob Storage.
list_containers(): Iterates over containers in the storage account.
REST API
You can also list containers directly using the REST API.
Request
Make a GET request to the containers endpoint.
GET https://<YOUR_STORAGE_ACCOUNT_NAME>.blob.core.windows.net/?comp=list&api-version=2019-07-07
Authorization: SharedKey :
Date: Tue, 29 Oct 2019 23:10:45 GMT
Content-Length: 0
Explanation
?comp=list: Specifies that you want to list containers.
api-version: The API version to use.
Authorization and Date headers are required for authentication. The signature is generated using your storage account key.
Example Response (XML)
my-container-1Tue, 29 Oct 2019 23:10:44 GMT0x8D75CAF921A660Aunlockedavailableoffdata-archiveMon, 28 Oct 2019 10:15:30 GMT0x8D75CA48B13B2E0unlockedavailableoff