Skip to content

Latest commit

 

History

History
115 lines (66 loc) · 7.06 KB

File metadata and controls

115 lines (66 loc) · 7.06 KB
title Create a blob container with Python
titleSuffix Azure Storage
description Learn how to create a blob container 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

Create a blob container with Python

[!INCLUDE storage-dev-guide-selector-create-container]

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 the Azure Storage client library for Python.

To learn about creating blob containers using asynchronous APIs, see Create a container asynchronously.

[!INCLUDE storage-dev-guide-prereqs-python]

Set up your environment

[!INCLUDE storage-dev-guide-project-setup-python]

Add import statements

Add the following import statements:

:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_create_container.py" id="Snippet_imports":::

Authorization

The authorization mechanism must have the necessary permissions to create a container. 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 Create Container (REST API).

[!INCLUDE storage-dev-guide-create-client-python]

[!INCLUDE storage-dev-guide-about-container-naming]

Create a container

To create a container, call the following method from the BlobServiceClient class:

You can also create a container using the following method from the ContainerClient 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 a BlobServiceClient object:

:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_create_container.py" id="Snippet_create_container":::

Create the root container

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 new ContainerClient object with the container name $root, then creates the container if it doesn't already exist in the storage account:

:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_create_container.py" id="Snippet_create_root_container":::

Create a container asynchronously

The Azure Blob Storage client library for Python supports creating a blob container asynchronously. To learn more about project setup requirements, see Asynchronous programming.

Follow these steps to create a container using asynchronous APIs:

  1. Add the following import statements:

    import asyncio
    
    from azure.identity.aio import DefaultAzureCredential
    from azure.storage.blob.aio import BlobServiceClient
    from azure.core.exceptions import ResourceExistsError
  2. Add code to run the program using asyncio.run. This function runs the passed coroutine, main() in our example, and manages the asyncio event loop. Coroutines are declared with the async/await syntax. In this example, the main() coroutine first creates the top level BlobServiceClient using async with, then calls the method that creates the container. Note that only the top level client needs to use async 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_create_container_async.py" id="Snippet_create_client_async":::

  3. Add code to create a container. The code is the same as the synchronous example, except that the method is declared with the async keyword and the await keyword is used when calling the create_container method.

    :::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_create_container_async.py" id="Snippet_create_container":::

With this basic setup in place, you can implement other examples in this article as coroutines using async/await syntax.

Resources

To learn more about creating a container using the Azure Blob Storage client library for Python, see the following resources.

Code samples

REST API operations

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:

[!INCLUDE storage-dev-guide-resources-python]

[!INCLUDE storage-dev-guide-next-steps-python]