Set or change a block blob's access tier with Python
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.
Prerequisites
- Azure subscription - create one for free
- Azure storage account - create a storage account
- Python 3.8+
Set up your environment
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, see Get 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 packages
Install the following packages using pip install
:
pip install azure-storage-blob azure-identity
Add import statements
Add the following import
statements:
from azure.identity import DefaultAzureCredential
from azure.storage.blob import (
BlobServiceClient,
BlobClient,
StandardBlobTier,
RehydratePriority
)
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.
Create a client object
To connect an app to Blob Storage, create an instance of BlobServiceClient. The following example shows how to create a client object using DefaultAzureCredential
for authorization:
# TODO: Replace <storage-account-name> with your actual storage account name
account_url = "https://<storage-account-name>.blob.core.windows.net"
credential = DefaultAzureCredential()
# Create the BlobServiceClient object
blob_service_client = BlobServiceClient(account_url, credential=credential)
You can also create client objects for specific containers or blobs, either directly or from the BlobServiceClient
object. To learn more about creating and managing client objects, see Create and manage client objects that interact with data resources.
About block blob access tiers
To manage costs for storage needs, it can be helpful to organize your data based on how frequently it's accessed and how long it needs to be retained. Azure storage offers different access tiers so that you can store your blob data in the most cost-effective manner based on how it's being used.
Access tiers for blob data
Azure Storage access tiers include:
- Hot tier - An online tier optimized for storing data that is accessed or modified frequently. The hot tier has the highest storage costs, but the lowest access costs.
- Cool tier - An online tier optimized for storing data that is infrequently accessed or modified. Data in the cool tier should be stored for a minimum of 30 days. The cool tier has lower storage costs and higher access costs compared to the hot tier.
- Cold tier - An online tier optimized for storing data that is infrequently accessed or modified. Data in the cold tier should be stored for a minimum of 90 days. The cold tier has lower storage costs and higher access costs compared to the cool tier.
- Archive tier - An offline tier optimized for storing data that is rarely accessed, and that has flexible latency requirements, on the order of hours. Data in the archive tier should be stored for a minimum of 180 days.
To learn more about access tiers, see Access tiers for blob data.
While a blob is in the Archive access tier, it's considered to be offline, and can't be read or modified. In order to read or modify data in an archived blob, you must first rehydrate the blob to an online tier. To learn more about rehydrating a blob from the Archive tier to an online tier, see Blob rehydration from the Archive tier.
Restrictions
Setting the access tier is only allowed on block blobs. To learn more about restrictions on setting a block blob's access tier, see Set Blob Tier (REST API).
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:
def upload_blob_access_tier(self, blob_service_client: BlobServiceClient, container_name: str, blob_name: str):
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
#Upload blob to the cool tier
with open(file=os.path.join(r'file_path', blob_name), mode="rb") as data:
blob_client = blob_client.upload_blob(data=data, overwrite=True, standard_blob_tier=StandardBlobTier.COOL)
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
:
def change_blob_access_tier(self, blob_client: BlobClient):
# Change the blob access tier to cool
blob_client.set_standard_blob_tier(StandardBlobTier.COOL)
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:
def rehydrate_blob_using_copy(self, source_archive_blob: BlobClient, destination_rehydrated_blob: BlobClient):
# Note: the destination blob must have a different name than the source blob
# Start the copy operation - specify the rehydrate priority and blob access tier
copy_operation = dict()
copy_operation = destination_rehydrated_blob.start_copy_from_url(
source_url=source_archive_blob.url,
standard_blob_tier=StandardBlobTier.HOT,
rehydrate_priority=RehydratePriority.STANDARD,
requires_sync=False)
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:
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 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 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.async def main(): sample = BlobAccessTierSamples() # TODO: Replace <storage-account-name> with an 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: # Change the blob access tier to cool blob_client = blob_service_client.get_blob_client(container="sample-container", blob="sample-blob.txt") await sample.change_blob_access_tier(blob_client=blob_client) if __name__ == '__main__': asyncio.run(main())
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 theawait
keyword is used when calling theset_standard_blob_tier
method.async def change_blob_access_tier(self, blob_client: BlobClient): # Change the blob access tier to cool await blob_client.set_standard_blob_tier(StandardBlobTier.COOL)
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:
- Set Blob Tier (REST API)
Client library resources
Code samples
- View synchronous or asynchronous code samples from this article (GitHub)