Skip to content

Commit 98e5f82

Browse files
authored
Merge pull request #261719 from pauljewellmsft/py-async
Add async sample for Python access tier article
2 parents 2009223 + 3fbf290 commit 98e5f82

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

articles/storage/blobs/storage-blob-use-access-tier-python.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.author: pauljewell
88

99
ms.service: azure-blob-storage
1010
ms.topic: how-to
11-
ms.date: 08/02/2023
11+
ms.date: 12/20/2023
1212
ms.devlang: python
1313
ms.custom: devx-track-python, devguide-python
1414
---
@@ -17,7 +17,9 @@ ms.custom: devx-track-python, devguide-python
1717

1818
[!INCLUDE [storage-dev-guide-selector-access-tier](../../../includes/storage-dev-guides/storage-dev-guide-selector-access-tier.md)]
1919

20-
This article shows how to set or change the access tier for a block blob using the [Azure Storage client library for Python](/python/api/overview/azure/storage).
20+
This article shows how to set or change the access tier for a block blob using the [Azure Storage client library for Python](/python/api/overview/azure/storage).
21+
22+
To learn about changing a blob's access tier using asynchronous APIs, see [Change a blob's access tier asynchronously](#change-a-blobs-access-tier-asynchronously).
2123

2224
## Prerequisites
2325

@@ -62,6 +64,37 @@ The following code example shows how to rehydrate an archived blob to the `Hot`
6264

6365
To learn more about copying a blob with Python, see [Copy a blob with Python](storage-blob-copy-python.md).
6466

67+
## Change a blob's access tier asynchronously
68+
69+
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](storage-blob-python-get-started.md#asynchronous-programming).
70+
71+
Follow these steps to change a blob's access tier using asynchronous APIs:
72+
73+
1. Add the following import statements:
74+
75+
```python
76+
import asyncio
77+
78+
from azure.storage.blob import (
79+
StandardBlobTier
80+
)
81+
from azure.identity.aio import DefaultAzureCredential
82+
from azure.storage.blob.aio import (
83+
BlobServiceClient,
84+
BlobClient
85+
)
86+
```
87+
88+
1. 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.
89+
90+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_access_tiers_async.py" id="Snippet_create_client_async":::
91+
92+
1. 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.
93+
94+
:::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":::
95+
96+
With this basic setup in place, you can implement other examples in this article as coroutines using async/await syntax.
97+
6598
## Resources
6699

67100
To learn more about setting access tiers using the Azure Blob Storage client library for Python, see the following resources.
@@ -76,7 +109,7 @@ The Azure SDK for Python contains libraries that build on top of the Azure REST
76109

77110
### Code samples
78111

79-
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_access_tiers.py)
112+
- View [synchronous](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_access_tiers.py) or [asynchronous](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_access_tiers_async.py) code samples from this article (GitHub)
80113

81114
### See also
82115

0 commit comments

Comments
 (0)