Azure-sdk-for-python: How do I determine if a storage container or blob exists?

Created on 27 Jul 2020  路  8Comments  路  Source: Azure/azure-sdk-for-python

In older versions of the Azure SDK for Python, I could call BaseBlobService.exists() to check if a container or blob exists.

I don't see any equivalent function in the documentation for BlobServiceClient, ContainerClient or BlobClient. The functions to create/overwrite a container or blob will throw an error if the object already exists, but there are times when I just want to verify something exists without modifying it.

What is the correct way to check if a blob exists in azure-storage-blob v12+?

Client Service Attention Storage customer-reported question

Most helpful comment

Hi @dharmab the exists method has been added to our SDK and will be available in this upcoming release!

All 8 comments

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @xgithubtriage.

Hi @dharmab

Thanks for reaching out!
Currently you can use blob_client.get_blob_properties() and it will throw ResourceNotFoundError if the blob/container/blob_snapshot doesn't exist.

We have an feature request to add exists() api, so users will be less confused about this.

Hi @dharmab the exists method has been added to our SDK and will be available in this upcoming release!

Sorry to highjack this a little. I was trying to use

from azure.storage.blob import ContainerClient

client = ContainerClient("storage1234567", container_name="container-01")
client.get_container_properties()

to check if a container existed. I had expected either an immediate ResourceNotFound or otherwise some authorised message - basically anything that would allow me to fail fast. Instead the command runs for about 2 minutes, retrying twice (three times in total, I believe with some backoff) resulting in

azure.core.exceptions.ServiceRequestError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7f7bd6ef8bb0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution

I have also tried all the timeout kwargs I can think of, timeout, read_timeout, connection_timeout, socket_timeout (supplied as numeric or tuple) and none make a difference.

Will the exists method short-circuit this and allow me to catch some faster failure?

Thanks.

PS: Please advise if I should instead open another issue!

Hi @AkhilGNair, interesting issue you've come across, but this is actually an unexpected behavior. The error you're getting implies the request was not sent (you would receive a ResourceNotFound response from the server if the request was sent). I believe this is an unrelated issue.
I would advise opening up another issue for it :)

Thanks @tasherif-msft, done!

Just FYI, I dont believe the azure docs have been updated to reflect that this new exists() method is available (unless I'm looking in the wrong location):
https://docs.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.blobclient?view=azure-python

Was this page helpful?
0 / 5 - 0 ratings