Skip to content

Commit 484490c

Browse files
Merge pull request #259771 from pauljewellmsft/py-dev-guide-samples
Add Python async samples for blob properties, metadata, and tags
2 parents e95e578 + 3e96295 commit 484490c

File tree

2 files changed

+92
-12
lines changed

2 files changed

+92
-12
lines changed

articles/storage/blobs/storage-blob-properties-metadata-python.md

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services: storage
66
author: pauljewellmsft
77

88
ms.author: pauljewell
9-
ms.date: 08/02/2023
9+
ms.date: 11/29/2023
1010
ms.service: azure-blob-storage
1111
ms.topic: how-to
1212
ms.devlang: python
@@ -19,6 +19,8 @@ ms.custom: devx-track-python, devguide-python
1919

2020
In addition to the data they contain, blobs support system properties and user-defined metadata. This article shows how to manage system properties and user-defined metadata using the [Azure Storage client library for Python](/python/api/overview/azure/storage).
2121

22+
To learn about managing properties and metadata using asynchronous APIs, see [Set blob metadata asynchronously](#set-blob-metadata-asynchronously).
23+
2224
## Prerequisites
2325

2426
- This article assumes you already have a project set up to work with the Azure Blob Storage client library for Python. To learn about setting up your project, including package installation, adding `import` statements, and creating an authorized client object, see [Get started with Azure Blob Storage and Python](storage-blob-python-get-started.md).
@@ -51,15 +53,15 @@ Any properties not explicitly set are cleared. To preserve any existing properti
5153

5254
The following code example sets the `content_type` and `content_language` system properties on a blob, while preserving the existing properties:
5355

54-
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_set_blob_properties":::
56+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs-properties-metadata-tags.py" id="Snippet_set_blob_properties":::
5557

5658
To retrieve properties on a blob, use the following method:
5759

5860
- [BlobClient.get_blob_properties](/python/api/azure-storage-blob/azure.storage.blob.blobclient#azure-storage-blob-blobclient-get-blob-properties)
5961

6062
The following code example gets a blob's system properties and displays some of the values:
6163

62-
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_get_blob_properties":::
64+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs-properties-metadata-tags.py" id="Snippet_get_blob_properties":::
6365

6466
## Set and retrieve metadata
6567

@@ -69,13 +71,51 @@ You can specify metadata as one or more name-value pairs on a blob or container
6971

7072
The following code example sets metadata on a blob:
7173

72-
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_set_blob_metadata":::
74+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs-properties-metadata-tags.py" id="Snippet_set_blob_metadata":::
7375

7476
To retrieve metadata, call the [get_blob_properties](/python/api/azure-storage-blob/azure.storage.blob.blobclient#azure-storage-blob-blobclient-get-blob-properties) method on your blob to populate the metadata collection, then read the values, as shown in the example below. The `get_blob_properties` method retrieves blob properties and metadata by calling both the [Get Blob Properties](/rest/api/storageservices/get-blob-properties) operation and the [Get Blob Metadata](/rest/api/storageservices/get-blob-metadata) operation.
7577

7678
The following code example reads metadata on a blob and prints each key/value pair:
7779

78-
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_get_blob_metadata":::
80+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs-properties-metadata-tags.py" id="Snippet_get_blob_metadata":::
81+
82+
## Set blob metadata asynchronously
83+
84+
The Azure Blob Storage client library for Python supports managing blob properties and metadata asynchronously. To learn more about project setup requirements, see [Asynchronous programming](storage-blob-python-get-started.md#asynchronous-programming).
85+
86+
Follow these steps to set blob metadata using asynchronous APIs:
87+
88+
1. Add the following import statements:
89+
90+
```python
91+
import asyncio
92+
93+
from azure.identity.aio import DefaultAzureCredential
94+
from azure.storage.blob.aio import BlobServiceClient
95+
```
96+
97+
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 sets the blob metadata. Note that only the top level client needs to use `async with`, as other clients created from it share the same connection pool.
98+
99+
```python
100+
async def main():
101+
sample = BlobSamples()
102+
103+
# TODO: Replace <storage-account-name> with your actual storage account name
104+
account_url = "https://<storage-account-name>.blob.core.windows.net"
105+
credential = DefaultAzureCredential()
106+
107+
async with BlobServiceClient(account_url, credential=credential) as blob_service_client:
108+
await sample.set_metadata(blob_service_client, "sample-container")
109+
110+
if __name__ == '__main__':
111+
asyncio.run(main())
112+
```
113+
114+
1. Add code to set the blob metadata. 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 `get_blob_properties` and `set_blob_metadata` methods.
115+
116+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs-properties-metadata-tags-async.py" id="Snippet_set_blob_metadata":::
117+
118+
With this basic setup in place, you can implement other examples in this article as coroutines using async/await syntax.
79119

80120
## Resources
81121

@@ -92,6 +132,6 @@ The Azure SDK for Python contains libraries that build on top of the Azure REST
92132

93133
### Code samples
94134

95-
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py)
135+
- View [synchronous](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob-devguide-blobs-properties-metadata-tags.py) or [asynchronous](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob-devguide-blobs-properties-metadata-tags-async.py) code samples from this article (GitHub)
96136

97137
[!INCLUDE [storage-dev-guide-resources-python](../../../includes/storage-dev-guides/storage-dev-guide-resources-python.md)]

articles/storage/blobs/storage-blob-tags-python.md

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services: storage
66
author: pauljewellmsft
77

88
ms.author: pauljewell
9-
ms.date: 08/02/2023
9+
ms.date: 11/29/2023
1010
ms.service: azure-blob-storage
1111
ms.topic: how-to
1212
ms.devlang: python
@@ -19,6 +19,8 @@ ms.custom: devx-track-python, devguide-python
1919

2020
This article shows how to use blob index tags to manage and find data using the [Azure Storage client library for Python](/python/api/overview/azure/storage).
2121

22+
To learn about setting blob index tags using asynchronous APIs, see [Set blob index tags asynchronously](#set-blob-index-tags-asynchronously).
23+
2224
## Prerequisites
2325

2426
- This article assumes you already have a project set up to work with the Azure Blob Storage client library for Python. To learn about setting up your project, including package installation, adding `import` statements, and creating an authorized client object, see [Get started with Azure Blob Storage and Python](storage-blob-python-get-started.md).
@@ -43,11 +45,11 @@ You can set tags by using the following method:
4345

4446
The specified tags in this method will replace existing tags. If old values must be preserved, they must be downloaded and included in the call to this method. The following example shows how to set tags:
4547

46-
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_set_blob_tags":::
48+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs-properties-metadata-tags.py" id="Snippet_set_blob_tags":::
4749

4850
You can delete all tags by passing an empty `dict` object into the `set_blob_tags` method:
4951

50-
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_clear_blob_tags":::
52+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs-properties-metadata-tags.py" id="Snippet_clear_blob_tags":::
5153

5254
## Get tags
5355

@@ -59,7 +61,7 @@ You can get tags by using the following method:
5961

6062
The following example shows how to retrieve and iterate over the blob's tags:
6163

62-
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_get_blob_tags":::
64+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs-properties-metadata-tags.py" id="Snippet_get_blob_tags":::
6365

6466
## Filter and find data with blob index tags
6567

@@ -74,7 +76,45 @@ You can find data by using the following method:
7476

7577
The following example finds and lists all blobs tagged as an image:
7678

77-
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py" id="Snippet_find_blobs_by_tags":::
79+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs-properties-metadata-tags.py" id="Snippet_find_blobs_by_tags":::
80+
81+
## Set blob index tags asynchronously
82+
83+
The Azure Blob Storage client library for Python supports working with blob index tags asynchronously. To learn more about project setup requirements, see [Asynchronous programming](storage-blob-python-get-started.md#asynchronous-programming).
84+
85+
Follow these steps to set blob index tags using asynchronous APIs:
86+
87+
1. Add the following import statements:
88+
89+
```python
90+
import asyncio
91+
92+
from azure.identity.aio import DefaultAzureCredential
93+
from azure.storage.blob.aio import BlobServiceClient
94+
```
95+
96+
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 sets the blob index tags. Note that only the top level client needs to use `async with`, as other clients created from it share the same connection pool.
97+
98+
```python
99+
async def main():
100+
sample = BlobSamples()
101+
102+
# TODO: Replace <storage-account-name> with your actual storage account name
103+
account_url = "https://<storage-account-name>.blob.core.windows.net"
104+
credential = DefaultAzureCredential()
105+
106+
async with BlobServiceClient(account_url, credential=credential) as blob_service_client:
107+
await sample.set_blob_tags(blob_service_client, "sample-container")
108+
109+
if __name__ == '__main__':
110+
asyncio.run(main())
111+
```
112+
113+
1. Add code to set the blob index tags. 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 `get_blob_tags` and `set_blob_tags` methods.
114+
115+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob-devguide-blobs-properties-metadata-tags-async.py" id="Snippet_set_blob_tags":::
116+
117+
With this basic setup in place, you can implement other examples in this article as coroutines using async/await syntax.
78118

79119
## Resources
80120

@@ -90,7 +130,7 @@ The Azure SDK for Python contains libraries that build on top of the Azure REST
90130

91131
### Code samples
92132

93-
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob-devguide-blobs.py)
133+
- View [synchronous](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob-devguide-blobs-properties-metadata-tags.py) or [asynchronous](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob-devguide-blobs-properties-metadata-tags-async.py) code samples from this article (GitHub)
94134

95135
[!INCLUDE [storage-dev-guide-resources-python](../../../includes/storage-dev-guides/storage-dev-guide-resources-python.md)]
96136

0 commit comments

Comments
 (0)