Recording from the fourth lesson (13/Mar 2018) of Cloud Applications Development course (NSWI152) for MMF UK (LS 2017/2018). It is published on our HAVIT YouTube Channel.
- Azure Storage Account
- Azure Storage – Blobs
- Azure Storage – Queues
You can find the labs instructions on GitHub (LAB4 + LAB5).
Having only a storage account (
CloudBlobClient) and a blob URI makes it a little bit tricky to check if the blob actually exists. To be able to use the
ICloudBlob.Exists() method, you have to have a blob-reference and there is no easy way to get it from URI.
If you just need to know if the blob exists, you can make a simple HTTP HEAD request to the URI with any HTTP client.
If you need the blob-reference for further use, there is a
CloudBlobClient.GetBlobReferenceFromServer(Uri blobUri) method. As you call it, it hits the server with a HEAD request and throws an exception if the blob does not exist (so there is no chance to use the
Exists method later).
You can catch this exception with a nice pattern-matching:
blob = blobClient.GetBlobReferenceFromServer(new Uri(url));
catch (Microsoft.WindowsAzure.Storage.StorageException ex)
when ((ex.InnerException is System.Net.WebException wex)
&& (wex.Response is System.Net.HttpWebResponse httpWebResponse)
&& (httpWebResponse.StatusCode == System.Net.HttpStatusCode.NotFound))
// blob does not exist, do whatever you need here
// further code able to use the blob-reference