Share via


使用 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 asyncio
    
    from azure.identity.aio import DefaultAzureCredential
    from azure.storage.blob.aio import BlobServiceClient
    
  2. 新增程式碼以使用 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.windows.net"
        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())
    
  3. 新增程式碼以刪除容器。 該程式碼與同步範例相同,不同之處在於該方法是以 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 操作

適用於 Python 的 Azure SDK 包含建置在 Azure REST API 之上的程式庫,可讓您透過熟悉的 Python 範例與 REST API 作業進行互動。 用來刪除或還原容器的用戶端程式庫方法會使用下列 REST API 作業:

程式碼範例

用戶端程式庫資源

另請參閱