Appending content in block blob

Topics: Semantic Logging Application Block, Windows Azure Integration Pack
Nov 21, 2014 at 1:13 PM
I have created these two method in azure custom sink for semantic logging.

used azure sdk 2.0

Code for Adding content to azure block blob

private void Post(EventEntry entry, string content)
    {
        List<string> blockIds = new List<string>();
        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve reference to a previously created container.
        CloudBlobContainer container = blobClient.GetContainerReference(ContainerName);


        // Create the container if it doesn't already exist.
        container.CreateIfNotExists();


        string fileName = DateTime.Now.ToShortDateString();

        CloudBlockBlob blockBlob = container.GetBlockBlobReference(string.Format("{0}_{1}", ContainerName, fileName));

        if (!blockBlob.Exists())
        {
            var bytesToUpload = Encoding.UTF8.GetBytes(content);

            using (var ms = new MemoryStream(bytesToUpload))
            {
                blockBlob.UploadFromStream(ms);
            }
        }
        else
        {
            blockBlob.AppendText(content);
        }
    }
}

Code for appending content to azure blob

public static void AppendText(this CloudBlockBlob blob, string text)
    {

        var bytesToUpload = Encoding.UTF8.GetBytes(text);

        List<string> blockIds = new List<string>();

        if (blob.Exists())
        {
            blockIds.AddRange(blob.DownloadBlockList(BlockListingFilter.Committed).Select(b => b.Name));

            var newId = Convert.ToBase64String(Encoding.Default.GetBytes(blockIds.Count.ToString()));


            blob.PutBlock(newId, new MemoryStream(bytesToUpload), null);

            blockIds.Add(newId);

            blob.PutBlockList(blockIds);
        }
    }


I have created two method for writing content to azure blob and append content to same blob.

This was working fine for some time. It is some time keep giving
  • web proxy authentication issue
    and
  • StorageClientException - The specified block list is invalid.
What is the best solution to fix this azure source code issue. kindly help.
Nov 21, 2014 at 7:20 PM
Seems like you are hitting some environmental and Azure issues. For the latter, it sounds like an issue related to the size of the blockId. See: https://stackoverflow.com/questions/10280739/writing-to-azure-block-blobs

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to