Skip to content
Advertisement

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

I have thousands of blobs in the container and have a list of blobs to be deleted fetched from some db.

Now, there is no guarantee that the id/name present in the list is present in the blob. so want to do a deleteIfExist kind of a scenario and

error it throws when blob is not present is NOT : azure.core.exceptions.ResourceNotFoundError: Operation which i can write an exception block for.

It throws something generic like : azure.storage.blob._shared.response_handlers.PartialBatchErrorException: There is a partial failure in the batch operation.

Code below :

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])

Advertisement

Answer

Take a look at the following code:

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("==============================")

Essentially whenever there an exception of type PartialBatchErrorException, it’s parts property will have the details about the result of each operation in the batch. You can check the status code of each operation to determine if it was successful (status code 202) or failed (other status code). If the status code is 404 (Not Found), you can assume that the blob does not exist.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement