Listing Blobs in Azure Storage

This document explains how to list blobs within a container in Azure Blob Storage using various SDKs and tools. Listing blobs is a fundamental operation for managing your storage data.

Understanding Blob Listing

When you list blobs, you can retrieve a list of blob names, their metadata, and properties. Azure Storage supports several options for listing, including:

  • Listing blobs at the root of a container.
  • Listing blobs within a specified virtual directory (prefix).
  • Using delimiters to simulate directory structures.
  • Specifying the maximum number of results to return.
  • Retrieving blob metadata and properties.

Methods for Listing Blobs

Using Azure CLI

The Azure Command-Line Interface (CLI) provides a straightforward way to list blobs. Use the az storage blob list command.

Azure CLI

az storage blob list \
    --account-name  \
    --account-key  \
    --container-name  \
    --output table
                    

Replace <your-storage-account-name>, <your-storage-account-key>, and <your-container-name> with your specific details. The --output table option provides a human-readable format.

Using .NET SDK

The Azure Blob Storage client library for .NET allows programmatic listing of blobs.

C#

using Azure.Storage.Blobs;
using System;

public class BlobLister
{
    public static void ListBlobs(string connectionString, string containerName)
    {
        BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

        Console.WriteLine("Listing blobs:");
        foreach (BlobItem blobItem in containerClient.GetBlobs())
        {
            Console.WriteLine($"- {blobItem.Name}");
        }
    }
}
                    

Using Python SDK

The Azure Blob Storage client library for Python offers similar functionality.

Python

from azure.storage.blob import BlobServiceClient

def list_blobs(connection_string, container_name):
    blob_service_client = BlobServiceClient.from_connection_string(connection_string)
    container_client = blob_service_client.get_container_client(container_name)

    print("Listing blobs:")
    for blob in container_client.list_blobs():
        print(f"- {blob.name}")

                    

Advanced Listing Options

Listing with a Prefix (Virtual Directories)

To list blobs within a specific "folder" (prefix), use the prefix parameter.

Python

# ... (previous code) ...
    for blob in container_client.list_blobs(name_starts_with="myfolder/"):
        print(f"- {blob.name}")
                    

Using Delimiters

Simulate directory structures by using a delimiter (e.g., '/') in your listing request. This is useful for listing "directories" and their contents.

Python

# ... (previous code) ...
    blob_list = container_client.list_blobs(delimiter='/')
    for blob_or_prefix in blob_list:
        if blob_or_prefix.get('is_prefix', False): # This is a virtual directory
            print(f"Directory: {blob_or_prefix['name']}")
        else: # This is a blob
            print(f"Blob: {blob_or_prefix['name']}")
                    

Blob Properties and Metadata

When listing blobs, you can also retrieve additional information:

Property Description
Name The name of the blob.
Size The size of the blob in bytes.
LastModified The date and time the blob was last modified.
Metadata Custom metadata associated with the blob.
Note: For very large numbers of blobs, consider using pagination to retrieve results in chunks to avoid memory issues and improve performance. The SDKs typically handle this automatically or provide mechanisms for manual control.
Tip: When using virtual directories, ensure your blob names are structured consistently with your chosen delimiter (e.g., data/images/image1.jpg).