Listing Blobs with Python

This guide demonstrates how to list blobs in an Azure Blob Storage container using the Azure Storage SDK for Python.

Prerequisites

Before you begin, ensure you have the following:

Authentication

You can authenticate with your storage account using a connection string, a shared access signature (SAS), or Azure Active Directory (AAD) credentials. For simplicity, this example uses a connection string.

Security Note: Avoid hardcoding connection strings directly in your code. Use environment variables or Azure Key Vault for secure storage.

Listing Blobs

To list blobs, you'll need to create a BlobServiceClient object and then obtain a ContainerClient for the container you want to access. The ContainerClient provides methods to interact with blobs within that container.

Step 1: Import necessary classes

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

Step 2: Define connection string and container name

Replace the placeholder values with your actual connection string and container name.

connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
            container_name = "your-container-name"

Step 3: Create a BlobServiceClient and ContainerClient

blob_service_client = BlobServiceClient.from_connection_string(connection_string)
            container_client = blob_service_client.get_container_client(container_name)

Step 4: List blobs in the container

The list_blobs() method returns an iterator of blob properties. You can iterate through this to get information about each blob.

print(f"Listing blobs in container: {container_name}")
            blob_list = container_client.list_blobs()

            for blob in blob_list:
                print(f"  - Blob name: {blob.name}")
                print(f"    Blob type: {blob.blob_type}")
                print(f"    Size: {blob.size} bytes")
                print(f"    Last modified: {blob.last_modified}")
                print("-" * 20)

Full Example Code

Here's the complete Python script:

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import os

# Replace with your actual connection string and container name
connection_string = os.environ.get("AZURE_STORAGE_CONNECTION_STRING", "YOUR_AZURE_STORAGE_CONNECTION_STRING")
container_name = "your-container-name"

def list_blobs_in_container(container_client: ContainerClient, container_name: str):
    """Lists all blobs in a specified container."""
    print(f"Listing blobs in container: {container_name}")
    try:
        blob_list = container_client.list_blobs()
        found_blobs = False
        for blob in blob_list:
            found_blobs = True
            print(f"  - Blob name: {blob.name}")
            print(f"    Blob type: {blob.blob_type}")
            print(f"    Size: {blob.size} bytes")
            print(f"    Last modified: {blob.last_modified}")
            print("-" * 20)
        if not found_blobs:
            print("  No blobs found in this container.")
    except Exception as ex:
        print(f"An error occurred: {ex}")

if __name__ == "__main__":
    if connection_string == "YOUR_AZURE_STORAGE_CONNECTION_STRING":
        print("Please set the AZURE_STORAGE_CONNECTION_STRING environment variable or replace the placeholder.")
    else:
        try:
            blob_service_client = BlobServiceClient.from_connection_string(connection_string)
            container_client = blob_service_client.get_container_client(container_name)
            list_blobs_in_container(container_client, container_name)
        except Exception as ex:
            print(f"Could not connect to storage account or retrieve container: {ex}")
            print("Please ensure your connection string and container name are correct and the container exists.")

Listing Blobs with Prefixes

You can also list blobs that match a specific prefix, which is useful for simulating folder structures.

# ... (previous code)

            prefix = "my-folder/" # Example prefix for a virtual folder
            print(f"\nListing blobs with prefix '{prefix}' in container: {container_name}")
            blob_list_with_prefix = container_client.list_blobs(name_starts_with=prefix)

            for blob in blob_list_with_prefix:
                print(f"  - {blob.name}")
            

Listing Blobs Recursively

By default, list_blobs() only lists blobs at the top level of the container. To list blobs in subfolders (simulated by prefixes), you can iterate through prefixes.

Tip: Azure Blob Storage does not have true folders. Hierarchical structures are simulated using prefixes in blob names.

Next Steps

Now that you know how to list blobs, you can explore other operations such as: