2
votes

I'm trying to perform a textbook append to an append blob in azure.

First I create a blob container. I know this operation succeeds because I can see the container in the storage explorer.

Next I create the blob. I know this operation succeeds because I can see the blob in the storage explorer.

Finally I attempt to append to the blob with the following code.

        var csa = CloudStorageAccount.Parse(BLOB_CONNECTION_STRING);
        var client = csa.CreateCloudBlobClient();
        var containerRefernece = client.GetContainerReference(CONTAINER_NAME);
        var blobrefernce = containerRefernece.GetAppendBlobReference(BLOB_NAME);

        var ms = new MemoryStream();
        var sr = new StreamWriter(ms);

        sr.WriteLine(message);
        ms.Seek(0, SeekOrigin.Begin);
        await blobrefernce.AppendBlockAsync(ms);

No matter what I do I get the following exception.

windowsAzure.Storage StorageException: The value for one of the HTTP headers is not in the correct format.

I'm at a bit of a loss as to how to proceed. I cant even determine what parameters are the problem from the exception. The connection string is copied directly from the azure portal. Note I am using the latest version (9.3.0) of the WindowsAzure.Storage NuGet package.

Any ideas how I can figure out what the problem is?

Thanks!

1

1 Answers

2
votes

Just add sr.Flush(); after sr.WriteLine(message); to make buffered data written to the underlying stream immediately.

AutoFlush of StreamWriter is false by default, so buffered data won't be written to destination until we use Flush or Close.

We need to use the MemoryStream which is the construct parameter of StreamWriter, so we can't use Close otherwise we will get exception like Cannot access a closed Stream.