Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"exists" operator for containers and blobs #9507

Closed
subesokun opened this issue Jan 17, 2020 · 8 comments
Closed

"exists" operator for containers and blobs #9507

subesokun opened this issue Jan 17, 2020 · 8 comments
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. feature-request This issue requires a new behavior in the product in order be resolved. Service Attention Workflow: This issue is responsible by Azure service team. Storage Storage Service (Queues, Blobs, Files)
Milestone

Comments

@subesokun
Copy link

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 Azure/azure-sdk-for-js#5062

@kaerm kaerm added Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. Storage Storage Service (Queues, Blobs, Files) labels Jan 17, 2020
@kaerm
Copy link
Contributor

kaerm commented Jan 17, 2020

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

@rakshith91
Copy link
Contributor

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

@subesokun
Copy link
Author

subesokun commented Jan 21, 2020

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

@xiafu-msft
Copy link
Contributor

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

@xiafu-msft xiafu-msft self-assigned this Feb 19, 2020
@Petermarcu Petermarcu added the Service Attention Workflow: This issue is responsible by Azure service team. label Apr 17, 2020
@ghost
Copy link

ghost commented Apr 17, 2020

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

@lmazuel lmazuel added the feature-request This issue requires a new behavior in the product in order be resolved. label May 4, 2020
@lmazuel lmazuel added this to the Backlog milestone May 5, 2020
@chrisyeh96
Copy link
Contributor

chrisyeh96 commented Jul 14, 2020

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)

@xiafu-msft
Copy link
Contributor

Hi @chrisyeh96
We have a pr to add blob exists() and it should be released soon this is the duplicate issue #12744

@tasherif-msft tasherif-msft self-assigned this Nov 17, 2020
@tasherif-msft
Copy link
Contributor

Both container.exists() (releasing soon) and blob.exists() are now available. I will close this issue :)

@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. feature-request This issue requires a new behavior in the product in order be resolved. Service Attention Workflow: This issue is responsible by Azure service team. Storage Storage Service (Queues, Blobs, Files)
Projects
None yet
Development

No branches or pull requests

8 participants