Azure-sdk-for-python: "exists" operator for containers and blobs

Created on 17 Jan 2020  路  7Comments  路  Source: Azure/azure-sdk-for-python

Add exists operator to ContainerClient and BlobClient to verify if the given container or blob is existing.

container_client = blob_service_client.get_container_client("mycontainer")
container_client.exists()

blob_container_client = container_client.get_blob_client("myblob")
blob_container_client.exists()

See related feature request for azure-sdk-for-js https://github.com/Azure/azure-sdk-for-js/issues/5062

Client Service Attention Storage customer-reported feature-request

Most helpful comment

Are there any updates on this? I would really appreciate a BlobClient.exists() method for checking if a blob exists within a given container, especially since this was a feature in v2.1 of the Azure Python SDK.

For the record, here's what I currently am doing to check for whether a blob exists, but it's far more verbose than it really should be.

from azure.storage.blob import BlobClient
from azure.core.exceptions import ResourceNotFoundError

def exists(blob_url: str) -> bool:
    blob_client = BlobClient.from_blob_url(blob_url)
    try:
        blob_client.get_blob_properties()
    except ResourceNotFoundError:
        return False
    return True

valid_blob_uri = "https://lilablobssc.blob.core.windows.net/nacti-unzipped/part0/sub000/2010_Unit150_Ivan097_img0003.jpg"
invalid_blob_uri = "https://lilablobssc.blob.core.windows.net/nacti-unzipped/part0/sub000/2010_Unit150_Ivan000_img0003.jpg"

assert exists(valid_blob_uri)
assert not exists(invalid_blob_uri)

All 7 comments

Hi @subesokun thank you for letting us know, we will look into this //cc: @rakshith91 @mayurid

Hi @subesokun
Thanks for reporting. While we discuss this feature, you can do the following if you are blocked because of it

from azure.core.exceptions import ResourceNotFoundError

try:
    # somethhing with blob/container
except ResourceNotFoundError:
    pass

@rakshith91 Thanks for the hint! This is a bit off-topic, but is there any documentation available that tells me which exceptions can be thrown by a certain function call? That would've helped me a lot to figure out on my own that e.g. ResourceNotFoundError gets thrown when accessing a none existing blob.

Hi @subesokun
Sorry about the late response.
You can map the http status code according to this chart, it would help you debug.
All of the exceptions are children of StorageErrorException

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

Are there any updates on this? I would really appreciate a BlobClient.exists() method for checking if a blob exists within a given container, especially since this was a feature in v2.1 of the Azure Python SDK.

For the record, here's what I currently am doing to check for whether a blob exists, but it's far more verbose than it really should be.

from azure.storage.blob import BlobClient
from azure.core.exceptions import ResourceNotFoundError

def exists(blob_url: str) -> bool:
    blob_client = BlobClient.from_blob_url(blob_url)
    try:
        blob_client.get_blob_properties()
    except ResourceNotFoundError:
        return False
    return True

valid_blob_uri = "https://lilablobssc.blob.core.windows.net/nacti-unzipped/part0/sub000/2010_Unit150_Ivan097_img0003.jpg"
invalid_blob_uri = "https://lilablobssc.blob.core.windows.net/nacti-unzipped/part0/sub000/2010_Unit150_Ivan000_img0003.jpg"

assert exists(valid_blob_uri)
assert not exists(invalid_blob_uri)

Hi @chrisyeh96
We have a pr to add blob exists() and it should be released soon this is the duplicate issue https://github.com/Azure/azure-sdk-for-python/issues/12744

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lmazuel picture lmazuel  路  3Comments

jmlero picture jmlero  路  3Comments

AmudhaPalani picture AmudhaPalani  路  4Comments

Fajkowsky picture Fajkowsky  路  4Comments

przemolb picture przemolb  路  4Comments