List Blobs in Azure Storage

This article explains how to list blobs in an Azure Storage container using the Azure SDKs and Azure CLI.

Overview

Listing blobs is a fundamental operation when working with Azure Blob Storage. You might need to list blobs to display them in a user interface, process them in a batch, or simply to get an inventory of your storage.

Prerequisites

  • An Azure Storage account.
  • A container within your storage account.
  • Appropriate permissions to access the container (e.g., Shared Access Signature (SAS) token or account key).

Using Azure SDKs

The Azure SDKs provide client libraries for various programming languages that simplify interacting with Azure services. Here are examples for C#, Python, and Node.js.

C# Example

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System;
using System.Threading.Tasks;

public class ListBlobs
{
    public static async Task ListAllBlobs(string blobUri, string containerName, string blobServiceClientConnectionString)
    {
        // Use the connection string to get a BlobServiceClient
        BlobServiceClient blobServiceClient = new BlobServiceClient(blobServiceClientConnectionString);

        // Get a BlobContainerClient
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

        // List all blobs in the container
        Console.WriteLine("Blobs in container: {0}", containerName);
        await foreach (var blobItem in containerClient.GetBlobsAsync())
        {
            Console.WriteLine("\t{0}", blobItem.Name);
        }
    }
}

Python Example

from azure.storage.blob import BlobServiceClient
from azure.core.exceptions import ResourceNotFoundError

def list_blobs_in_container(connection_string, container_name):
    """Lists all blobs in a specified container."""
    try:
        blob_service_client = BlobServiceClient.from_connection_string(connection_string)
        container_client = blob_service_client.get_container_client(container_name)

        print(f"Blobs in container: {container_name}")
        blob_list = container_client.list_blobs()
        for blob in blob_list:
            print(f"\t{blob.name}")

    except ResourceNotFoundError:
        print(f"Container '{container_name}' not found.")
    except Exception as ex:
        print(f"An error occurred: {ex}")

# Example usage:
# list_blobs_in_connection_string("YOUR_AZURE_STORAGE_CONNECTION_STRING", "my-container")

Node.js Example

const { BlobServiceClient } = require("@azure/storage-blob");

async function listBlobs(connectionString, containerName) {
    // Create a BlobServiceClient
    const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);

    // Get a container client
    const containerClient = blobServiceClient.getContainerClient(containerName);

    // List blobs
    console.log(`Blobs in container: ${containerName}`);
    for await (const blobItem of containerClient.listBlobs()) {
        console.log(`\t${blobItem.name}`);
    }
}

// Example usage:
// listBlobs("YOUR_AZURE_STORAGE_CONNECTION_STRING", "my-container");

Using Azure CLI

The Azure CLI provides a command-line interface for managing Azure resources. You can list blobs using the az storage blob list command.

# Replace with your storage account name and container name
az storage blob list \
  --account-name mystorageaccount \
  --container-name mycontainer \
  --output table

This command will output a table listing the blobs in the specified container.

Advanced Listing Options

Both the SDKs and Azure CLI offer options for filtering and paginating results. You can list blobs recursively within virtual directories, filter by prefix, and control the number of items returned per request.

Tip: For large containers, consider using prefix filtering to manage the number of blobs returned in a single request. This can improve performance and reduce costs.

API Reference

Method/Command Description
BlobContainerClient.GetBlobsAsync() (C#) Retrieves a collection of blobs within a container.
container_client.list_blobs() (Python) Returns an iterable of blob properties for blobs in the container.
containerClient.listBlobs() (Node.js) Returns an async iterable of blob properties for blobs in the container.
az storage blob list (Azure CLI) Lists blobs within a storage container.