9

I noticed in the debug trace my current approach is triggering a HEAD on the blob - presumably to check if it already exists. I know it never will, and if it did exist I want to overwrite. I just want a straight PUT to shave a few milliseconds and importantly reduce noise in Application Insights Application map where HTTP 404 errors show up as failures on Azure Blob dependency.

There is no option within BlobHttpHeaders, neither can I find any such overwrite option in BlobUploadOptions. What am I missing ?

I want to do all the work of setting Content-Type and Tags in a single call. I am aware of the below override, but that would entail subsequent requests to set Content-Type and Tags which I want to avoid.

UploadAsync(Stream content, bool overwrite = false, CancellationToken cancellationToken = default)

SDK Assembly Azure.Storage.Blobs, Version=12.10.0.0

My current code:

                // blob settings
                var options = new BlobUploadOptions()
                {
                    // set content type
                    HttpHeaders = new BlobHttpHeaders()
                    {
                        ContentType = contentType,
                        ContentEncoding = "UTF-8"
                    },
                    // Set blob index tags
                    Tags = new Dictionary<string, string>
                    {
                        { "quoteid", "123" },
                        { "responseId","456" }
                    }
                };

                // upload blob
                var content = Encoding.UTF8.GetBytes(data);
                using var ms = new MemoryStream(content);
                await blobClient.UploadAsync(
                    ms,
                    options
                    );

Update:

  1. The HEAD is being triggered by BlobBaseClient.Exists
  2. Also asked on GitHub by someone else
7

1 Answer 1

1

As suggested by User Gaurav Mantri - Stack Overflow, use BlockBlobClient

Note: Partial updates are not supported with UploadAsync(Stream, BlobUploadOptions, CancellationToken); the content of the existing blob is overwritten with the content of the new blob. To perform a partial update of the content of a block blob, use the Azure.Storage.Blobs.Specialized.BlockBlobClient.StageBlock(System.String,System.IO.Stream,System.Byte[],Azure.Storage.Blobs.Models.BlobRequestConditions,System.IProgress{System.Int64},System.Threading.CancellationToken) and CommitBlockListAsync(IEnumerable, CommitBlockListOptions, CancellationToken) operations.

Reference: BlockBlobClient.UploadAsync(Stream, BlobUploadOptions, CancellationToken) Method (Azure.Storage.Blobs.Specialized) - Azure for .NET Developers | Microsoft Docs

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.