Skip to content

Latest commit

 

History

History
149 lines (82 loc) · 8.93 KB

File metadata and controls

149 lines (82 loc) · 8.93 KB
title Create and manage container leases with Python
titleSuffix Azure Storage
description Learn how to manage a lock on a container in your Azure Storage account using the Python client library.
services storage
author stevenmatthew
ms.author shaas
ms.service azure-blob-storage
ms.topic how-to
ms.date 08/05/2024
ms.devlang python
ms.custom devx-track-python, devguide-python

Create and manage container leases with Python

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

This article shows how to create and manage container leases using the Azure Storage client library for Python. You can use the client library to acquire, renew, release and break container leases.

To learn about leasing a blob container using asynchronous APIs, see Lease containers 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_lease_container.py" id="Snippet_imports":::

Authorization

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

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

About container leases

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

Lease operations are handled by the BlobLeaseClient class, which provides a client containing all lease operations for blobs and containers. To learn more about blob leases using the client library, see Create and manage blob leases with Python.

Acquire a lease

When you acquire a container lease, you obtain a lease ID that your code can use to operate on the container. If the container already has an active lease, you can only request a new lease by using the active lease ID. However, you can specify a new lease duration.

To acquire a lease, create an instance of the BlobLeaseClient class, and then use the following method:

You can also acquire a lease using the following method from the ContainerClient class:

The following example acquires a 30-second lease on a container:

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

Renew a lease

You can renew a container lease if the lease ID specified on the request matches the lease ID associated with the container. The lease can be renewed even if it has expired, as long as the container hasn't been leased again since the expiration of that lease. When you renew a lease, the duration of the lease resets.

To renew a lease, use the following method:

The following example renews a lease for a container:

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

Release a lease

You can release a container lease if the lease ID specified on the request matches the lease ID associated with the container. Releasing a lease allows another client to acquire a lease for the container immediately after the release is complete.

You can release a lease by using the following method:

The following example releases the lease on a container:

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

Break a lease

You can break a container lease if the container has an active lease. Any authorized request can break the lease; the request isn't required to specify a matching lease ID. A lease can't be renewed after it's broken, and breaking a lease prevents a new lease from being acquired for a period of time until the original lease expires or is released.

You can break a lease by using the following method:

The following example breaks the lease on a container:

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

Lease containers asynchronously

The Azure Blob Storage client library for Python supports leasing containers asynchronously. To learn more about project setup requirements, see Asynchronous programming.

Follow these steps to lease 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, BlobLeaseClient
  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 acquires the container lease. 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_lease_container_async.py" id="Snippet_create_client_async":::

  3. Add code to acquire a container lease. 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 acquire method.

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

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

[!INCLUDE storage-dev-guide-container-lease]

Resources

To learn more about leasing 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 leasing a container use the following REST API operation:

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

See also

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