4

If I have a random URL, how would I check if it points to a blob in Azure? If it points to an Azure Blob I can be sure that this blob exists and that I can access it.
I tried the following:

  1. I made some tests with Exists, but it's not very satisfying:

    new CloudBlockBlob(new Uri("http://example.com"))
    ArgumentException: Invalid blob address 'http://example.com/', missing container information
    
    new CloudBlockBlob(new Uri("https://example.com/fdh/3746C9A2-533E-4544-A10B-321A8BC40AEA/sample-file.txt")).Exists()
    false
    
    new CloudBlockBlob(new Uri("http://stackoverflow.com/questions/43109843/ways-to-migrate-documents-pdf-forms-from-ms-sharepoint-to-aem")).Exists()
    StorageException: Blob type of the blob reference doesn't match blob type of the blob.
    

    So basically creating the CloudBlockBlob can fail, Exists can fail or return false.

    But it just feels wrong to do it like this.

  2. I could check the url to be https://<storage-account>.blob.core.windows.net/<container>/*, but I'm not sure if this is always correct.

If I could guarantee that 2. works I would do this because no HTTP request is necessary.

Are there other (better) ways to check if a random URL points to a blob in Azure (maybe something out of the (Azure Storage SDK) box)?

4
  • Is making network request an option? Commented Mar 30, 2017 at 6:39
  • Yes, of course, any request is ok, as long as it does the job. Commented Mar 30, 2017 at 6:44
  • Well it can also fail if it is a private container and/or you haven't specified the right credentials Commented Mar 30, 2017 at 6:46
  • OK, if I have a URL that points to an Azure Blob, I can be sure that it exists and I can access it (updated post). Commented Mar 30, 2017 at 6:47

1 Answer 1

2

Not really an elegant solution (read, it's a hack basically :D)

One thing you could do is make a HEAD request to the URL. What you would need to do is parse the response headers. There are 3 scenarios that you would need to consider:

  1. URL points to a blob in blob storage and is publicly accessible.
  2. URL points to a blob in blob storage but is not publicly accessible (container is private).
  3. URL does not point to blob storage.

Now every request to Azure Blob Storage will have a response header called x-ms-request-id which contains a GUID like value. You can use this to differentiate between scenarios where URL points to blob storage and URL doesn't point to blob storage. Of course this check will fail (and that's why the solution is more of a "hack" if the URL actually returns this header [some random site decides to include this exact header in response]).

In case #1, you will get status code 200 back. Because the blob is publicly accessible you will get additional headers back. One of the headers there would be x-ms-blob-type and that could potentially have 3 values: BlockBlob, AppendBlob or PageBlob.

In case #2, you will get status code 404 back however you will receive x-ms-request-id response header back.

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

5 Comments

That's right, it's a hack :-), but an interesting one anyway. Thanks.
I'm using this approach in the last year or so and it's working great. Note that you could also get 401 unauthorized
@yonisha I don't think for blob storage you will ever get 401 status code back. Can you please explain under what scenario you will get 401 back?
@GauravMantri, sorry I meant 403. When using invalid SAS token Azure will return "403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature". Creating a blob url with a valid SAS and changing the SAS will result with this error
@yonisha...Yes, 403 error can definitely come under numerous scenarios. But even in this case you will get x-ms-request-id header back.

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.