DCSIMG
Does a Blob Exist? - Manu Cohen-Yashar's Blog

Manu Cohen-Yashar's Blog

Does a Blob Exist?

One of the most common task relating to blobs is a simple check to verify if it exist. Unfortunately there is no method or property for CloudBlob that provides an answer.

I wrote a simple extension method to do that:

         public static bool Exists(this CloudBlob blob)
        {
            try
            {
                blob.FetchAttributes();
                return true;
            }
            catch (StorageClientException e)
            {
                if (e.ErrorCode == StorageErrorCode.ResourceNotFound)
                {
                    return false;
                }
                throw;
            }
        }

but when I run the method I got sometimes an InvalidOperationException "BlobType of the blob reference doesn't match BlobType of the blob". No one can explain this including Steve Marx but many developers have seen this happens. So I changed my code to:

        public static bool Exists(this CloudBlob blob)
        {
            try
            {
                if (blob is CloudBlockBlob)
                {
                    ((CloudBlockBlob)blob).FetchAttributes();
                    return true;
                }
                if (blob is CloudPageBlob)
                {
                    ((CloudPageBlob)blob).FetchAttributes();
                    return true;
                }
                try
                {
                    (blob).FetchAttributes();
                    return true;
                }
                catch (InvalidOperationException ex)
                {
                    if (ex.Message == "BlobType of the blob reference doesn't match BlobType of the blob")
                        return true;
                    throw;
                }
                
            }
            catch (StorageClientException e)
            {
                if (e.ErrorCode == StorageErrorCode.ResourceNotFound)
                {
                    return false;
                }
                throw;
            }
        }

Enjoy

Manu

Comments

Windows Azure and Cloud Computing Posts for 5/25/2012+ - Windows Azure Blog said:

Pingback from  Windows Azure and Cloud Computing Posts for 5/25/2012+ - Windows Azure Blog

# May 27, 2012 4:05 AM

#Azure Action ??? Community Newsletter ??? 30th May 2012 | MSDN Blogs said:

Pingback from  #Azure Action ??? Community Newsletter ??? 30th May 2012 | MSDN Blogs

# May 31, 2012 12:01 AM

Wallen said:

If your oven thermometer does not match your oven temperature setting, you will

want to have your oven calibrated. When you are baking bread, you can

raise the bread dough in the cold oven and then just turn on the oven to the correct temperature once the

bread is raised. Now you're ready to be creative with the outside of the cake.

# March 21, 2013 11:40 AM

Babcock said:

Wow, this article is good, my sister is analyzing such things,

thus I am going to inform her.

# March 22, 2013 2:10 PM

Higgs said:

After 5 minutes, turn the pizza 180 degrees to insure even cooking.

Sprinkle the top with Italian seasonings for garnish and additional

flavor. The other example of a place where you can get your own pizza coupons is the pizza hut.

# March 24, 2013 8:29 AM

Merchant said:

Be watchful with washing soda and use gloves as it is really alkaline.

- Centre tunnel chimney duct to maximise successful scorching airflow.

* Chinese pizza: Top unbaked pizza dough with hoisin sauce, sliced green peppers and onions, and sauteed

shitake mushrooms.

# March 25, 2013 1:51 AM

Street said:

Place a silpat liner on a baking sheet (or spray with nonstick cooking spray).

You can often buy these at home improvement stores, in

the outdoor grilling sections. If you have a double oven, set both up the

same way.

# March 28, 2013 6:14 PM

asdf said:

Seriously?  If you're going to catch and then check the exception, why cast it at all?  It just makes your code ugly.

# May 8, 2013 2:29 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: