0

I have a directory in azure with filenames like topview/<some id>_topview.png (i.e. within topview directory). The files are image files. I want to wrote a script which, given a list of fetches the image files which exists in azure.

   blob_service_client = BlobServiceClient.from_connection_string(imagedata_acc_string)
   container_client = blob_service_client.get_container_client(imagedata_acc_container)

   topviewfilepath =  "gt" + "/" + id + "_topview.png" #
   blobfilepath = "topview/" + id + "_topview.png"    

   blob_client = container_client.get_blob_client(blobfilepath)
   with open(topviewfilepath, "wb") as data:
      myblob = blob_client.download_blob()
      myblob.readinto(data)

I want to check if the blob exists in azure before downloading it (because if it doesnt exist, it throws an error) and stops.

I tried blob_client.exist() but it is just the client and not the file. I want to check if the file in itself exists.

I checked these links but still cant figure the exact command:

  1. Checking if a blob exist in python azure
  2. How to check if a folder exist or not in Azure container using Python?
  3. Checking if a blob exists in Azure Storage (this is in C#)
Sulphur
  • 514
  • 6
  • 24
  • `I tried blob_client.exist() but it is just the client and not the file. I want to check if the file in itself exists.` - Can you elaborate more? – Gaurav Mantri Jul 19 '22 at 18:00
  • Sure. When I see the value of `blob.client` in debugger mode, it always returns me a value. They look like a big string but it doesn't help me as I still don't know if file exists or not. `blob_client.exists()` is always returning me True but I can see that blob doesnt exists because download fails and throws and error – Sulphur Jul 19 '22 at 18:10
  • 1
    That's weird! `blob_client.exists()` should make a network call and check the existence of the blob and return true/false accordingly. What's the error you are getting when you are trying to download the blob? – Gaurav Mantri Jul 19 '22 at 18:15
  • Ohh, my bad. You are right. One of syntax were wrong. Just found the error. Thank you for the help. Will close it now. – Sulphur Jul 19 '22 at 18:19

0 Answers0