Skip to content

Instantly share code, notes, and snippets.

@xiaomi7732
Created January 28, 2022 01:44
Show Gist options
  • Select an option

  • Save xiaomi7732/ab1d403ef497047fcb4d419e1c64f5cb to your computer and use it in GitHub Desktop.

Select an option

Save xiaomi7732/ab1d403ef497047fcb4d419e1c64f5cb to your computer and use it in GitHub Desktop.
Upload Blob with BlobUploadOptions + Allow overwrite

Overwrite + BlobOptions in 1 shot

The Azure Storage SDK doens't provide an overload to have both parameters for overwrting and a BlobUploadOptions. But that is a common scenario. By reading the implementations, it looks like it is easy to do:

using (Stream contentStream = File.OpenRead(@"C:\tmp\Test.txt"))
{
    BlobUploadOptions overwriteWithTags = new BlobUploadOptions
    {
        // Set tags, properties and so on & so force. This has nothing to do with overwrite.
        Tags = new Dictionary<string, string>
        {
            ["TagB"] = "b",
        },
        // By default, there will be condition to prevent overwrite.
        // Set the conditions to null so that overwrite will be allowed.
        Conditions = null,
    };
    await blobClient.UploadAsync(contentStream, overwriteWithTags);
}

It is twisted to find that not allowing overwrite is actually a SDK default, not a server one.

By default, the condition is:

// There is always going to be a match to fail this condition when the target blob exist.
new BlobRequestConditions
{
    IfNoneMatch = new ETag("*")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment