9

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

3 Answers 3

17

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)
Sign up to request clarification or add additional context in comments.

4 Comments

This method take nearly one sec to check if the blob exists, is there any faster way to achieve that ?
Can you try the async io variant. Updated answer to include code snippet for async.
The exists() method returns true even if the blob does not exist, which causes the blob.delete_blob() to error.
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

1 Comment

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.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.