使用 Python 删除和还原 Blob 容器

本文介绍了如何使用适用于 Python 的 Azure 存储客户端库删除容器。 如果已启用容器软删除,则可以还原已删除的容器。

要了解如何使用异步 API 删除 blob 容器,请参阅异步删除容器

先决条件

  • 本文假设已经设置了项目来使用适用于 Python 的 Azure Blob 存储客户端库。 要了解有关设置项目的信息,包括包安装、添加 import 语句和创建授权客户端对象,请参阅开始使用 Azure Blob 存储和 Python
  • 授权机制必须具有删除 blob 容器或还原软删除容器的权限。 若要了解详细信息,请参阅以下 REST API 操作的授权指南:

删除容器

若要在 Python 中删除容器,请使用 BlobServiceClient 类中的以下方法:

还可以使用 ContainerClient 类中的以下方法来删除容器:

删除容器后,至少在 30 秒内无法使用相同的名称创建容器。 尝试使用相同的名称创建容器将会失败,并出现 HTTP 错误代码 409 (Conflict)。 针对容器或其包含的 Blob 执行任何其他操作将会失败,并出现 HTTP 错误代码 404 (Not Found)

下面的示例使用 BlobServiceClient 对象删除指定容器:

def delete_container(self, blob_service_client: BlobServiceClient, container_name):
    container_client = blob_service_client.get_container_client(container=container_name)
    container_client.delete_container()

以下示例演示如何删除以指定的前缀开头的所有容器:

def delete_container_prefix(self, blob_service_client: BlobServiceClient):
    container_list = list(blob_service_client.list_containers(name_starts_with="test-"))
    assert len(container_list) >= 1

    for container in container_list:
        # Find containers with the specified prefix and delete
        container_client = blob_service_client.get_container_client(container=container.name)
        container_client.delete_container()

还原软删除的容器

为存储帐户启用容器软删除后,可以在指定的保持期内恢复删除的容器及其内容。 若要详细了解容器软删除,请参阅启用和管理容器的软删除。 可以通过调用 BlobServiceClient 类的以下方法来还原软删除的容器:

以下示例查找已删除的容器,获取该已删除容器的版本,然后将该版本传递到 undelete_container 方法以还原该容器。

def restore_deleted_container(self, blob_service_client: BlobServiceClient, container_name):
    container_list = list(
        blob_service_client.list_containers(include_deleted=True))
    assert len(container_list) >= 1

    for container in container_list:
        # Find the deleted container and restore it
        if container.deleted and container.name == container_name:
            restored_container_client = blob_service_client.undelete_container(
                deleted_container_name=container.name, deleted_container_version=container.version)

异步删除容器

适用于 Python 的 Azure Blob 存储客户端库支持异步删除 blob 容器。 要详细了解项目设置要求,请参阅异步编程

按照以下步骤使用异步 API 删除容器:

  1. 添加以下 import 语句:
import asyncio

from azure.identity.aio import DefaultAzureCredential
from azure.storage.blob.aio import BlobServiceClient
  1. 添加代码以使用asyncio.run运行程序。 此函数运行传递的协同例程(我们示例中的main()),并管理asyncio事件循环。 使用 async/await 语法声明协同例程。 在此示例中,main() 协同例程先使用 async with 创建顶级 BlobServiceClient,然后调用删除容器的方法。 请注意,只有顶级客户端需要使用async with,因为基于它创建的其他客户端会共享同一连接池。
async def main():
    sample = ContainerSamples()

    # TODO: Replace <storage-account-name> with your actual storage account name
    account_url = "https://<storage-account-name>.blob.core.chinacloudapi.cn"
    credential = DefaultAzureCredential()

    async with BlobServiceClient(account_url, credential=credential) as blob_service_client:
        await sample.delete_container(blob_service_client, "sample-container")

if __name__ == '__main__':
    asyncio.run(main())
  1. 添加用于删除容器的代码。 该代码与同步示例相同,除了该方法使用async关键字声明,await关键字在调用delete_container方法时使用。
async def delete_container(self, blob_service_client: BlobServiceClient, container_name):
    container_client = blob_service_client.get_container_client(container=container_name)
    await container_client.delete_container()

有了此基本设置,即可使用 async/await 语法将本文中的其他示例实现为协同例程。

资源

若要详细了解如何使用适用于 Python 的 Azure Blob 存储客户端库删除容器,请参阅以下资源。

REST API 操作

Azure SDK for Python 包含基于 Azure REST API 而生成的库,允许你通过熟悉的 Python 范例与 REST API 操作进行交互。 用于删除或还原容器的客户端库方法使用以下 REST API 操作:

代码示例

客户端库资源

请参阅