Skip to content

Latest commit

 

History

History
131 lines (77 loc) · 7.65 KB

File metadata and controls

131 lines (77 loc) · 7.65 KB
title Set or change a blob's access tier with Python
titleSuffix Azure Storage
description Learn how to set or change a blob's access tier 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

Set or change a block blob's access tier with Python

[!INCLUDE storage-dev-guide-selector-access-tier]

This article shows how to set or change the access tier for a block blob using the Azure Storage client library for Python.

To learn about changing a blob's access tier using asynchronous APIs, see Change a blob's access tier 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_access_tiers.py" id="Snippet_imports":::

Authorization

The authorization mechanism must have the necessary permissions to set a blob's access tier. 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 Set Blob Tier.

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

[!INCLUDE storage-dev-guide-about-access-tiers]

Note

To set the access tier to Cold using Python, you must use a minimum client library version of 12.15.0.

Set a blob's access tier during upload

You can set a blob's access tier on upload by passing the standard_blob_tier keyword argument to upload_blob or upload_blob_from_url.

The following code example shows how to set the access tier when uploading a blob:

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

To learn more about uploading a blob with Python, see Upload a blob with Python.

Change the access tier for an existing block blob

You can change the access tier of an existing block blob by using the following function:

The following code example shows how to change the access tier for an existing blob to Cool:

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

If you're rehydrating an archived blob, you can optionally pass the rehydrate_priority keyword argument as HIGH or STANDARD.

Copy a blob to a different access tier

You can change the access tier of an existing block blob by specifying an access tier as part of a copy operation. To change the access tier during a copy operation, pass the standard_blob_tier keyword argument to start_copy_from_url. If you're rehydrating a blob from the archive tier using a copy operation, you can optionally pass the rehydrate_priority keyword argument as HIGH or STANDARD.

The following code example shows how to rehydrate an archived blob to the Hot tier using a copy operation:

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

To learn more about copying a blob with Python, see Copy a blob with Python.

Change a blob's access tier asynchronously

The Azure Blob Storage client library for Python supports changing a blob's access tier asynchronously. To learn more about project setup requirements, see Asynchronous programming.

Follow these steps to change a blob's access tier using asynchronous APIs:

  1. Add the following import statements:

    import asyncio
    
    from azure.storage.blob import (
    StandardBlobTier
    )
    from azure.identity.aio import DefaultAzureCredential
    from azure.storage.blob.aio import (
    BlobServiceClient,
    BlobClient
    )
  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 changes the blob's access tier. 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_access_tiers_async.py" id="Snippet_create_client_async":::

  3. Add code to change the blob's access tier. 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 set_standard_blob_tier method.

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

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 setting access tiers using the Azure Blob Storage client library for Python, see the following resources.

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 setting access tiers use the following REST API operation:

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

Code samples

See also