Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

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

Feedback

In this article

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.

Prerequisites

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, 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 packages

Install the following packages usingpip install:

pip install azure-storage-blob azure-identity

Add import statements

Add the followingimport statements:

from azure.identity import DefaultAzureCredentialfrom 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 roleStorage Blob Data Contributor or higher. To learn more, see the authorization guidance forSet Blob Tier.

Create a client object

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.

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, 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.

Restrictions

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.

Set a blob's access tier during upload

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.

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 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.

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 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.

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, seeAsynchronous programming.

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

  1. 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)
  2. 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())
  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 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.

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:

Client library resources

Code samples

See also


Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?