This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Blobs in Azure Storage are organized into containers. Before you can upload a blob, you must first create a container. This article shows how to create containers with theAzure Storage client library for Python.
To learn about creating blob containers using asynchronous APIs, seeCreate a container asynchronously.
If you don't have an existing project, this section shows you how to set up a project to work with the Azure Blob Storage client library for Python. For more details, seeGet started with Azure Blob Storage and Python.
To work with the code examples in this article, follow these steps to set up your project.
Install the following packages usingpip install:
pip install azure-storage-blob azure-identityAdd the followingimport statements:
from azure.core.exceptions import ResourceExistsErrorfrom azure.identity import DefaultAzureCredentialfrom azure.storage.blob import BlobServiceClientThe authorization mechanism must have the necessary permissions to create a container. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in roleStorage Blob Data Contributor or higher. To learn more, see the authorization guidance forCreate Container (REST API).
To connect an app to Blob Storage, create an instance ofBlobServiceClient. The following example shows how to create a client object usingDefaultAzureCredential for authorization:
# TODO: Replace <storage-account-name> with your actual storage account nameaccount_url = "https://<storage-account-name>.blob.core.windows.net"credential = DefaultAzureCredential()# Create the BlobServiceClient objectblob_service_client = BlobServiceClient(account_url, credential=credential)You can also create client objects for specificcontainers orblobs, either directly or from theBlobServiceClient object. To learn more about creating and managing client objects, seeCreate and manage client objects that interact with data resources.
A container name must be a valid DNS name, as it forms part of the unique URI used to address the container or its blobs. Follow these rules when naming a container:
The URI for a container resource is formatted as follows:
https://my-account-name.blob.core.windows.net/my-container-name
To create a container, call the following method from theBlobServiceClient class:
You can also create a container using the following method from theContainerClient class:
Containers are created immediately beneath the storage account. It's not possible to nest one container beneath another. An exception is thrown if a container with the same name already exists.
The following example creates a container from aBlobServiceClient object:
def create_blob_container(self, blob_service_client: BlobServiceClient, container_name): try: container_client = blob_service_client.create_container(name=container_name) except ResourceExistsError: print('A container with this name already exists')A root container serves as a default container for your storage account. Each storage account may have one root container, which must be named$root. The root container must be explicitly created or deleted.
You can reference a blob stored in the root container without including the root container name. The root container enables you to reference a blob at the top level of the storage account hierarchy. For example, you can reference a blob in the root container as follows:
https://accountname.blob.core.windows.net/default.html
The following example creates a newContainerClient object with the container name $root, then creates the container if it doesn't already exist in the storage account:
def create_blob_root_container(self, blob_service_client: BlobServiceClient): container_client = blob_service_client.get_container_client(container="$root") # Create the root container if it doesn't already exist if not container_client.exists(): container_client.create_container()The Azure Blob Storage client library for Python supports creating a blob container asynchronously. To learn more about project setup requirements, seeAsynchronous programming.
Follow these steps to create a container using asynchronous APIs:
Add the following import statements:
import asynciofrom azure.identity.aio import DefaultAzureCredentialfrom azure.storage.blob.aio import BlobServiceClientfrom azure.core.exceptions import ResourceExistsErrorAdd code to run the program usingasyncio.run. This function runs the passed coroutine,main() in our example, and manages theasyncio event loop. Coroutines are declared with the async/await syntax. In this example, themain() coroutine first creates the top levelBlobServiceClient usingasync with, then calls the method that creates the container. Note that only the top level client needs to useasync with, as other clients created from it share the same connection pool.
async def main(): sample = ContainerSamples() # TODO: Replace <storage-account-name> with your actual storage account name account_url = "https://<storage-account-name>.blob.core.windows.net" credential = DefaultAzureCredential() async with BlobServiceClient(account_url, credential=credential) as blob_service_client: await sample.create_blob_container(blob_service_client, "sample-container")if __name__ == '__main__': asyncio.run(main())Add code to create a container. The code is the same as the synchronous example, except that the method is declared with theasync keyword and theawait keyword is used when calling thecreate_container method.
async def create_blob_container(self, blob_service_client: BlobServiceClient, container_name): try: container_client = await blob_service_client.create_container(name=container_name) except ResourceExistsError: print('A container with this name already exists')With this basic setup in place, you can implement other examples in this article as coroutines using async/await syntax.
To learn more about creating a container using the Azure Blob Storage client library for Python, see the following resources.
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 creating a container use the following REST API operation:
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?