7

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

RaphWork
  • 346
  • 1
  • 4
  • 14

3 Answers3

13

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)
krishg
  • 5,935
  • 2
  • 12
  • 19
  • This method take nearly one sec to check if the blob exists, is there any faster way to achieve that ? – RaphWork Sep 15 '20 at 07:17
  • 1
    Can you try the async io variant. Updated answer to include code snippet for async. – krishg Sep 15 '20 at 07:40
  • The exists method is missing in the docs: https://learn.microsoft.com/en-us/azure/developer/python/sdk/storage/azure-storage-blob/azure.storage.blob.blobclient?view=storage-py-v12#methods – dparkar Oct 03 '22 at 21:43
  • 1
    The exists() method returns true even if the blob does not exist, which causes the blob.delete_blob() to error. – Brady Feb 03 '23 at 19:39
0

as the @krshg suggested the easiest way to check the existance of the blob file is

from azure.storage.blob import BlobClient
conn_str = "my_connection_string"
my_blob_name = "my_dir/my_blob"


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

Howeverm, I have a smal problem to acquire the connection string. I am using the Azure Storage Explrer. If you too do so you can get the connection string by clicking on the storage and on the butum-left panel get the cconnection string : enter image description here Another thing is that if inside the container there is a directory that contain s your blob file you can easily use the path format to your file

Afshin Amiri
  • 3,438
  • 1
  • 20
  • 21
-1
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

  • 6
    Code only answers are discouraged. Please provide a summary of how your answer solves the problem and why it may be preferable to the other answers provided. – DaveL17 Apr 27 '21 at 11:32