Fishcake
Fishcake

Reputation: 10774

Check if Blob of unknown Blob type exists

I've inherited a project built using the Azure Storage Client 1.7 and am upgrading it as Microsoft have announced that this will no longer be supported from December this year.

References to the files in Blob storage are stored in a database with the following fields:

  1. FilePath - a string in the form of uploadfiles/xxx/yyy/Image-20140117170146.jpg

  2. FileURI - A string in the form of https://zzz.blob.core.windows.net/uploadfiles/xxx/yyy/Image-20140117170146.jpg

GetBlobReferenceFromServer will throw an exception if the file doesn't exist, so it seems you should use GetBlockBlobReference if you know the container and the Blob type.

So my question(s):

Upvotes: 0

Views: 598

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136346

Can I assume any Blobs currently uploaded (using StorageClient 1.7) will be BlockBlobs?

Though you can't be 100% sure that the blobs uploaded via Storage Client library 1.7 are Blob Blobs because 1.7 also supported Page Blobs however you can make some intelligent guesses. For example, if the files are image files and other commonly used files (pdf, document etc.), you can assume that they are block blobs. Typically you would see vhd files uploaded as page blobs. Again if these are uploaded by the users of your application, more than likely they are block blobs.

Having said this, I think you should use GetBlobReferenceFromServer method. What you could do is list all blobs from the database and for each of them call GetBlobReferenceFromServer method. If the blob exists, then you will get the blob type. If the blob doesn't exist, this method will give you an error. This would be the quickest way to identify the blob type of existing entries in the database. If you want, you can store the blob type back in the database along with existing record if you find both block and page blobs when you check the blob type so that if in future you need to decide between creating a CloudBlockBlob or CloudPageBlob reference, you can look at this field.

As I need to know the container name to call GetBlockBlobReference can I reliably say that in the examples above my container would always be uploadfiles

Yes. In the examples you listed above, you can say that the blob container is upload files.

Upvotes: 1

Related Questions