| 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 |
[!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]
[!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_access_tiers.py" id="Snippet_imports":::
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.
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.
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.
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.
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:
-
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 )
-
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 changes the blob's access tier. 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_access_tiers_async.py" id="Snippet_create_client_async":::
-
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
asynckeyword and theawaitkeyword is used when calling theset_standard_blob_tiermethod.:::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.
To learn more about setting access tiers 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 setting access tiers use the following REST API operation:
- Set Blob Tier (REST API)
[!INCLUDE storage-dev-guide-resources-python]
- View synchronous or asynchronous code samples from this article (GitHub)