Issues with adding text to Azure Storage Append Blob

Hello,

Azure Storage recently announced Append blob – more info here and here.

Append blob is great because allow you to add to the end of the blob content with only single operation. This is perfect for logging and auditing scenarios.

I start prototyping with it and my code initially was


var storageAccount = CloudStorageAccount.Parse(connectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
await container.CreateIfNotExistsAsync();
var appendBlob = container.GetAppendBlobReference(blobPath);
if (!await appendBlob.ExistsAsync())
{
   await appendBlob.CreateOrReplaceAsync();
}
await appendBlob.AppendTextAsync(text);

And this code when accessed by multiple threads eventually was throwing the following exception : “The remote server returned an error: (412) The append position condition specified was not met..”

This is strange since in one of the main characteristics of Append Blob is that “Append Blob supports having multiple clients writing to the same blob without any need for synchronization”.

Then I change the append operation to work with blocks instead of text and and it worked. So the final working code look like this:


var storageAccount = CloudStorageAccount.Parse(connectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
await container.CreateIfNotExistsAsync();
var appendBlob = container.GetAppendBlobReference(blobPath);
if (!await appendBlob.ExistsAsync())
{
   await appendBlob.CreateOrReplaceAsync();
}
using (var stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(text)))
{
   await appendBlob.AppendBlockAsync(stream);
}

I am still investigating what is the difference and I will update the post if new information is available.

In the mean time, please use AppendBlockAsync instead AppendTextAsync.

Thank you!

Kanio

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s