This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
This article shows how to set or change the access tier for a block blob using theAzure Storage client library for Python.
To learn about changing a blob's access tier using asynchronous APIs, seeChange a blob's access tier asynchronously.
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, seeGet 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 the following packages usingpip install:
pip install azure-storage-blob azure-identityAdd the followingimport statements:
from azure.identity import DefaultAzureCredentialfrom azure.storage.blob import ( BlobServiceClient, BlobClient, StandardBlobTier, RehydratePriority)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 roleStorage Blob Data Contributor or higher. To learn more, see the authorization guidance forSet Blob Tier.
To connect an app to Blob Storage, create an instance ofBlobServiceClient. The following example shows how to create a client object usingDefaultAzureCredential for authorization:
# TODO: Replace <storage-account-name> with your actual storage account nameaccount_url = "https://<storage-account-name>.blob.core.windows.net"credential = DefaultAzureCredential()# Create the BlobServiceClient objectblob_service_client = BlobServiceClient(account_url, credential=credential)You can also create client objects for specificcontainers orblobs, either directly or from theBlobServiceClient object. To learn more about creating and managing client objects, seeCreate and manage client objects that interact with data resources.
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.
Azure Storage access tiers include:
To learn more about access tiers, seeAccess 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, seeBlob rehydration from the Archive tier.
Setting the access tier is only allowed on block blobs. To learn more about restrictions on setting a block blob's access tier, seeSet Blob Tier (REST API).
Note
To set the access tier toCold using Python, you must use a minimumclient library version of 12.15.0.
You can set a blob's access tier on upload by passing thestandard_blob_tier keyword argument toupload_blob orupload_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, seeUpload 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 toCool:
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 therehydrate_priority keyword argument asHIGH orSTANDARD.
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 thestandard_blob_tier keyword argument tostart_copy_from_url. If you're rehydrating a blob from the archive tier using a copy operation, you can optionally pass therehydrate_priority keyword argument asHIGH orSTANDARD.
The following code example shows how to rehydrate an archived blob to theHot 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, seeCopy 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, seeAsynchronous programming.
Follow these steps to change a blob's access tier using asynchronous APIs:
Add the following import statements:
import asynciofrom azure.storage.blob import (StandardBlobTier)from azure.identity.aio import DefaultAzureCredentialfrom azure.storage.blob.aio import (BlobServiceClient,BlobClient)Add code to run the program usingasyncio.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 theasync 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.
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:
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?