Search code examples
c#azureconcurrencyblobetag

Forcing Etag check on Blob creation


Is there a way of enforcing Etag AccessCondition check for the first time data is uploaded to the blob (when the blob does not exist)?

I have multiple different servers updating the same blob. When the blob already exists, it's very easy to enforce the Etag AccessCondition on write and that works fine.

What I cannot do is enforce Etag check at blob creation time.

  • If I pass string.Empty or null as the Etag AccessCondition when I first create the blob, then it's free for all mode. Basically any server trying to write to the blob will always succeed whether it actually created the blob or just erased/overwrote the previous contents of the blob.
  • I am not able to FetchProperties() of a non-existing blob obviously so there is no initial state Etag.
  • Passing any non-empty string as Etag when the Blob still does not exist always fails on precondition check.

Solution

  • Found the solution to this here: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/a02ff6ab-dceb-4955-908b-7e39137e4774/concurrent-upload-to-azure-blob-using-same-key?forum=windowsazuredata

    So basically here are the two AccessConditions needed:

    1. If a blob does not exist use: AccessCondition.GenerateIfNoneMatchCondition("*")
    2. If the blob does exist use: AccessCondition.GenerateIfMatchCondition(_etag)

    "*" (asterisk character) is a special wildcard string used by Azure. The condition #1 says that the operation should be executed as long as the resource has no Etag values at all (which is only possible if the resource does not exist).