Skip to content

Commit ab115df

Browse files
Merge pull request #261696 from pauljewellmsft/py-async-container
Add async samples for Python container articles
2 parents 3ba7a58 + fc3f70e commit ab115df

File tree

2 files changed

+78
-11
lines changed

2 files changed

+78
-11
lines changed

articles/storage/blobs/storage-blob-container-delete-python.md

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: pauljewellmsft
77

88
ms.service: azure-blob-storage
99
ms.topic: how-to
10-
ms.date: 08/02/2023
10+
ms.date: 12/20/2023
1111
ms.author: pauljewell
1212
ms.devlang: python
1313
ms.custom: devx-track-python, devguide-python
@@ -19,6 +19,8 @@ ms.custom: devx-track-python, devguide-python
1919

2020
This article shows how to delete containers with the [Azure Storage client library for Python](/python/api/overview/azure/storage). If you've enabled [container soft delete](soft-delete-container-overview.md), you can restore deleted containers.
2121

22+
To learn about deleting a blob container using asynchronous APIs, see [Delete a container asynchronously](#delete-a-container-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).
@@ -40,11 +42,11 @@ After you delete a container, you can't create a container with the same name fo
4042

4143
The following example uses a `BlobServiceClient` object to delete the specified container:
4244

43-
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_containers.py" id="Snippet_delete_container":::
45+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_container.py" id="Snippet_delete_container":::
4446

4547
The following example shows how to delete all containers that start with a specified prefix:
4648

47-
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_containers.py" id="Snippet_delete_container_prefix":::
49+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_container.py" id="Snippet_delete_container_prefix":::
4850

4951
## Restore a deleted container
5052

@@ -54,7 +56,32 @@ When container soft delete is enabled for a storage account, a deleted container
5456

5557
The following example finds a deleted container, gets the version of that deleted container, and then passes the version into the `undelete_container` method to restore the container.
5658

57-
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_containers.py" id="Snippet_restore_container":::
59+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_container.py" id="Snippet_restore_container":::
60+
61+
## Delete a container asynchronously
62+
63+
The Azure Blob Storage client library for Python supports deleting a blob container asynchronously. To learn more about project setup requirements, see [Asynchronous programming](storage-blob-python-get-started.md#asynchronous-programming).
64+
65+
Follow these steps to delete a container using asynchronous APIs:
66+
67+
1. Add the following import statements:
68+
69+
```python
70+
import asyncio
71+
72+
from azure.identity.aio import DefaultAzureCredential
73+
from azure.storage.blob.aio import BlobServiceClient
74+
```
75+
76+
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 deletes the container. Note that only the top level client needs to use `async with`, as other clients created from it share the same connection pool.
77+
78+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_container_async.py" id="Snippet_create_client_async":::
79+
80+
1. Add code to delete the container. 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 `delete_container` method.
81+
82+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_delete_container_async.py" id="Snippet_delete_container":::
83+
84+
With this basic setup in place, you can implement other examples in this article as coroutines using async/await syntax.
5885

5986
## Resources
6087

@@ -69,7 +96,7 @@ The Azure SDK for Python contains libraries that build on top of the Azure REST
6996

7097
### Code samples
7198

72-
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_containers.py)
99+
- View [synchronous](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_delete_container.py) or [asynchronous](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_delete_container_async.py) code samples from this article (GitHub)
73100

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

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

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

88
ms.service: azure-blob-storage
99
ms.topic: how-to
10-
ms.date: 08/02/2023
10+
ms.date: 12/20/2023
1111
ms.author: pauljewell
1212
ms.devlang: python
1313
ms.custom: devx-track-python, devguide-python
@@ -19,6 +19,8 @@ ms.custom: devx-track-python, devguide-python
1919

2020
Blob containers support system properties and user-defined metadata, in addition to the data they contain. This article shows how to manage system properties and user-defined metadata with 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 container metadata asynchronously](#set-container-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).
@@ -43,7 +45,7 @@ To retrieve container properties, use the following method:
4345

4446
The following code example fetches a container's system properties and writes the property values to a console window:
4547

46-
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_containers.py" id="Snippet_get_container_properties":::
48+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_container_properties_metadata.py" id="Snippet_get_container_properties":::
4749

4850
## Set and retrieve metadata
4951

@@ -55,15 +57,53 @@ Setting container metadata overwrites all existing metadata associated with the
5557

5658
The following code example sets metadata on a container:
5759

58-
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_containers.py" id="Snippet_set_container_metadata":::
60+
:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-devguide-py/blob_devguide_container_properties_metadata.py" id="Snippet_set_container_metadata":::
5961

6062
To retrieve metadata, call the following method:
6163

6264
- [ContainerClient.get_container_properties](/python/api/azure-storage-blob/azure.storage.blob.containerclient#azure-storage-blob-containerclient-get-container-properties)
6365

6466
The following example reads in metadata values:
6567

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

68108
## Resources
69109

@@ -77,10 +117,10 @@ The Azure SDK for Python contains libraries that build on top of the Azure REST
77117
- [Set Container Metadata](/rest/api/storageservices/set-container-metadata) (REST API)
78118
- [Get Container Metadata](/rest/api/storageservices/get-container-metadata) (REST API)
79119

80-
The `get_container_properties` method retrieves container 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.
120+
The `get_container_properties` method retrieves container properties and metadata by calling both the [Get Container Properties](/rest/api/storageservices/get-container-properties) operation and the [Get Container Metadata](/rest/api/storageservices/get-container-metadata) operation.
81121

82122
### Code samples
83123

84-
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_containers.py)
124+
- View [synchronous](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_container_properties_metadata.py) or [asynchronous](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-devguide-py/blob_devguide_container_properties_metadata_async.py) code samples from this article (GitHub)
85125

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

0 commit comments

Comments
 (0)