1
votes

I'm getting a HTTP 400 Bad Request when trying to append bytes to an append blob in Azure Storage. The Azure Storage Account already exists.

The Azure documentation only has an example call to the AppendText() method, but this method includes a warning about using this method with multiple client writers. Hence, I would like to use the AppendBlock() method instead, to ensure that calls are atomic.

var cred = new StorageCredentials("storageaccountname", "storageaccountkey");
var account = new CloudStorageAccount(cred, true);

var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("csharp");
container.CreateIfNotExists();

var log = container.GetAppendBlobReference("artofshell.log");
var stream = new MemoryStream(1024);
var text = System.Text.Encoding.ASCII.GetBytes("Log entry #1");
stream.Write(text, 0, text.Length);

log.CreateOrReplace();
log.AppendBlock(stream);

Any ideas what could be causing this?

  • Windows 10 Enterprise Insider Build 14332
  • Visual Studio 2015
  • WindowsAzure.Storage library 7.0.1-preview
1

1 Answers

2
votes

To fix this issue, please reset the stream's position just before calling AppendBlock(). So your code would be:

        log.CreateOrReplace();
        stream.Position = 0;//Reset stream's position
        log.AppendBlock(stream);

The reason you're getting this error is because you're trying to send 0 bytes. Because the stream is positioned at the end, length of the content you're trying to send is 0 and storage service doesn't like it :). When I ran your code and traced the request/response in Fiddler, I got 400 error as well with following details:

<?xml version="1.0" encoding="utf-8"?>
    <Error>
        <Code>InvalidHeaderValue</Code>
        <Message>
            The value for one of the HTTP headers is not in the correct format.
            RequestId:b265553a-0001-0072-6e58-ae8e34000000
            Time:2016-05-15T03:15:51.0349150Z
        </Message>
        <HeaderName>
            Content-Length
        </HeaderName>
        <HeaderValue>
            0
        </HeaderValue>
    </Error>