3
votes

Since the new update of azure-storage-blob, the blockblobservice is depreciated

How can I check that a blob exist ?

This answer is not working with the new version of azure-storage-blob Faster Azure blob name search with python?

I found this issue on GitHub : https://github.com/Azure/azure-sdk-for-python/issues/12744

2

2 Answers

6
votes

Version 12.5.0 released on 2020-09-10 has now the exists method in the new SDK.

For example,

Sync:

from azure.storage.blob import BlobClient

blob = BlobClient.from_connection_string(conn_str="my_connection_string", container_name="mycontainer", blob_name="myblob")
exists = blob.exists()
print(exists)

Async:

import asyncio

async def check():
    from azure.storage.blob.aio import BlobClient
    blob = BlobClient.from_connection_string(conn_str="my_connection_string", container_name="mycontainer", blob_name="myblob")
    async with blob:
        exists = await blob.exists()
        print(exists)
0
votes
from azure.storage.blob import BlobServiceClient
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
all_containers = blob_service_client.list_containers()
print([container['name'] for container in all_containers])

The above piece of code will list all the containers from where we can just check the existence of a container. But please note that the output is paged so in case you have a large number of containers, you need to iterate through the output using the continuation token. Please refer for more info: https://github.com/Azure/azure-storage-python/blob/54e60c8c029f41d2573233a70021c4abf77ce67c/azure-storage-blob/azure/storage/blob/baseblobservice.py#L573