| title | List blob containers with Python |
|---|---|
| titleSuffix | Azure Storage |
| description | Learn how to list blob containers in your Azure Storage account using the Python client library. |
| services | storage |
| author | stevenmatthew |
| ms.service | azure-blob-storage |
| ms.topic | how-to |
| ms.date | 08/05/2024 |
| ms.author | shaas |
| ms.devlang | python |
| ms.custom | devx-track-python, devguide-python |
[!INCLUDE storage-dev-guide-selector-list-container]
When you list the containers in an Azure Storage account from your code, you can specify several options to manage how results are returned from Azure Storage. This article shows how to list containers using the Azure Storage client library for Python.
To learn about listing blob containers using asynchronous APIs, see List containers asynchronously.
[!INCLUDE storage-dev-guide-prereqs-python]
[!INCLUDE storage-dev-guide-project-setup-python]
Add the following import statements:
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_list_containers.py" id="Snippet_imports":::
The authorization mechanism must have the necessary permissions to list blob containers. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Contributor or higher. To learn more, see the authorization guidance for List Containers (REST API).
[!INCLUDE storage-dev-guide-create-client-python]
When listing containers from your code, you can specify options to manage how results are returned from Azure Storage. You can specify the number of results to return in each set of results, and then retrieve the subsequent sets. You can also filter the results by a prefix, and return container metadata with the results. These options are described in the following sections.
To list containers in a storage account, call the following method:
This method returns an iterable of type ContainerProperties. Containers are ordered lexicographically by name.
By default, a listing operation returns up to 5000 results at a time. To return a smaller set of results, provide a nonzero value for the results_per_page keyword argument.
To filter the list of containers, specify a string or character for the name_starts_with keyword argument. The prefix string can include one or more characters. Azure Storage then returns only the containers whose names start with that prefix.
To include container metadata with the results, set the include_metadata keyword argument to True. Azure Storage includes metadata with each container returned, so you don't need to fetch the container metadata separately.
To include soft-deleted containers with the results, set the include_deleted keyword argument to True.
The following example lists all containers and metadata. You can include container metadata by setting include_metadata to True:
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_list_containers.py" id="Snippet_list_containers":::
The following example lists only containers that begin with a prefix specified in the name_starts_with parameter:
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_list_containers.py" id="Snippet_list_containers_prefix":::
You can also specify a limit for the number of results per page. This example passes in results_per_page and paginates the results:
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_list_containers.py" id="Snippet_list_containers_pages":::
The Azure Blob Storage client library for Python supports listing containers asynchronously. To learn more about project setup requirements, see Asynchronous programming.
Follow these steps to list containers using asynchronous APIs:
-
Add the following import statements:
import asyncio from azure.identity.aio import DefaultAzureCredential from azure.storage.blob.aio import BlobServiceClient
-
Add code to run the program using
asyncio.run. This function runs the passed coroutine,main()in our example, and manages theasyncioevent loop. Coroutines are declared with the async/await syntax. In this example, themain()coroutine first creates the top levelBlobServiceClientusingasync with, then calls the method that lists the containers. Note that only the top level client needs to useasync with, as other clients created from it share the same connection pool.:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_list_containers_async.py" id="Snippet_create_client_async":::
-
Add code to list the containers. The code is the same as the synchronous example, except that the method is declared with the
asynckeyword andasync foris used when calling thelist_containersmethod.:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_list_containers_async.py" id="Snippet_list_containers":::
With this basic setup in place, you can implement other examples in this article as coroutines using async/await syntax.
To learn more about listing containers using the Azure Blob Storage client library for Python, see the following resources.
- View synchronous or asynchronous code samples from this article (GitHub)
The Azure SDK for Python contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Python paradigms. The client library methods for listing containers use the following REST API operation:
- List Containers (REST API)
[!INCLUDE storage-dev-guide-resources-python]
[!INCLUDE storage-dev-guide-next-steps-python]