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

How do I determine if a storage container or blob exists? #12744

Closed
dharmab opened this issue Jul 27, 2020 · 10 comments · Fixed by #13221
Closed

How do I determine if a storage container or blob exists? #12744

dharmab opened this issue Jul 27, 2020 · 10 comments · Fixed by #13221
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. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team. Storage Storage Service (Queues, Blobs, Files)

Comments

@dharmab
Copy link

dharmab commented Jul 27, 2020

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+?

@ghost ghost added needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Jul 27, 2020
@kaerm kaerm added Service Attention Workflow: This issue is responsible by Azure service team. Storage Storage Service (Queues, Blobs, Files) labels Jul 27, 2020
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Jul 27, 2020
@ghost
Copy link

ghost commented Jul 27, 2020

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

@kaerm kaerm added the Client This issue points to a problem in the data-plane of the library. label Jul 27, 2020
@xiafu-msft
Copy link
Contributor

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.

@tasherif-msft
Copy link
Contributor

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

@AkhilGNair
Copy link

AkhilGNair commented Sep 11, 2020

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!

@tasherif-msft
Copy link
Contributor

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 :)

@AkhilGNair
Copy link

Thanks @tasherif-msft, done!

@danielmustafa
Copy link

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

@tasherif-msft
Copy link
Contributor

openapi-sdkautomation bot pushed a commit to AzureSDKAutomation/azure-sdk-for-python that referenced this issue Feb 9, 2021
[AML] Add PipelineEndpoint with version and DataPathAssignments fields in AzureMLExecutePipeline (Azure#12744)

* Update Pipeline.json

Add PipelineEndpoint with version and DataPathAssignments fields in AzureMLExecutePipeline

* update

* update

* nit
@pshashipreetham
Copy link

I Tried Follwoing Code, It Didn't work for me

from azure.storage.blob import BlobClient
blob_client = BlobClient("url of Storage account", "container name", "Blob Name", snapshot=None, credential="credential of Storage Account")
print(blob_client.exists()) 

I got the Following Error:

ValueError: Unable to determine account name for shared key credential.

@pshashipreetham
Copy link

Even this is giving the same error:

test = ContainerClient("{}://{}.dfs.core.windows.net".format("https", "storage account name"), "container name", credential="Storage Account")
print(test.list_blobs(name_starts_with="blob name"))

@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. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team. Storage Storage Service (Queues, Blobs, Files)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants