0

Is there a way to know if a blob file exists inside a container without getting the whole list of blob objects ?

Thanks,

3 Answers 3

2

If you know the address of the blob, a tip from the Azure SDK is to first build a CloudBlockBlob (or a CloudPageBlob) and then call FetchAttributes. This call will throw a StorageClientException if it cannot locate the blob.

From the CloudBlobClient.GetBlockBlobReference documentation:

The FetchAttributes method executes a HEAD request to populate the blob's properties and metadata and as such is a lightweight option for determining whether the blob exists.

Sign up to request clarification or add additional context in comments.

Comments

1

Starting from Windows Azure Storage Client Library 2.0, the blob contains method Exists(), e.g: blob.Exists()

the same is true for the BlobContainer.

Comments

0

This is the code that I'm using

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

3 Comments

Has this changed with SDK 1.7?
This is no longer relevant, since in SDK2.x (2.3) they have removed it. They made a complete shit out of the library!!! stackoverflow.com/questions/14363564/… is showing how to handle these exceptions, but the Error code is inside a prop, that is NULL most of the time, which will THROW an exception when you try to handle the code !!! The fall-back is to use the status codes! Great improvement, right? The new SDK is awesome!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.