Python 中 Azure blob 删除客户端中 Blob 不存在场景的处理异常

Handle exception for Blob not present scenario in Azure blob delete client in Python

我在容器中有数千个 blob,并且有一个从某些数据库中获取的要删除的 blob 列表。

现在,不能保证列表中的 id/name 存在于 blob 中。所以想做一个 deleteIfExist 类型的场景并且

当 blob 不存在时抛出的错误不是:azure.core.exceptions.ResourceNotFoundError:我可以为其编写异常块的操作。

它抛出一些通用的东西,例如:azure.storage.blob._shared.response_handlers.PartialBatchErrorException:批处理操作部分失败。

代码如下:

from azure.storage.blob import BlobServiceClient
blob_service_client = BlobServiceClient.from_connection_string(conn_str_for_list)
container="name1-entity"
# Instantiate a ContainerClient
container_client = blob_service_client.get_container_client(container)

file_name = "something.txt"
fileobj = open(file_name, "r")
entityIdsList = [line.rstrip() for line in fileobj]
fileobj.close()

blobs_list = entitysChunk
    print(blobs_list)

    blobs_length = len(blobs_list)
# container_client.delete_blobs(*blobs_list)
    if blobs_length <= 256:
        container_client.delete_blobs(*blobs_list)

    else:
        start = 0
        end = 256

        while end <= blobs_length:
        # each time, delete 256 blobs at most
            container_client.delete_blobs(*blobs_list[start:end])
            start = start + 256
            end = end + 256
            if start < blobs_length and end > blobs_length:
                container_client.delete_blobs(*blobs_list[start:blobs_length])

看看下面的代码:

from azure.storage.blob import BlobServiceClient,PartialBatchErrorException
conn_str_for_list = "connection-string"
blob_service_client = BlobServiceClient.from_connection_string(conn_str_for_list)
container="blob-container-name"

container_client = blob_service_client.get_container_client(container)

file_name = "blobs.txt"
fileobj = open(file_name, "r")
entityIdsList = [line.rstrip() for line in fileobj]
fileobj.close()

blobs_list = entityIdsList

print(blobs_list)

try:
    result = container_client.delete_blobs(*blobs_list)
    for item in result:
        print(item.status_code)
except PartialBatchErrorException as e:
    print(e.message)
    print("-----------------------")
    print(e.response)
    print("-----------------------")
    print(e.parts)
    print("-----------------------")
    for part in e.parts:
        if (part.status_code == 202):
            print("Blob delete request was accepted.")
        elif (part.status_code == 404):
            print("Blob does not exist. Consider it deleted.")
        else:
            print("Something else happened. You better take a look at it.")
            print(part)
        print("==============================")

本质上,每当出现 PartialBatchErrorException 类型的异常时,它的 parts 属性 将包含有关批处理中每个操作的结果的详细信息。您可以检查每个操作的状态代码以确定它是成功 (status code 202) 还是失败 (other status code)。如果状态代码是 404 (Not Found),则可以假设该 blob 不存在。