Search code examples
azureazure-blob-storageazure-storage

Azure Blob AppendBlobClient.AppendBlob() throwing an exception


For the code:

AppendBlobClient.AppendBlob(Stream) 

I am sometimes getting the exception:

The value for one of the HTTP headers is not in the correct format.
RequestId:9d7754b2-4e3d-4256-97f8-f77b43ab3756
Time:2023-12-13T19:36:11.536Z
Status: 400 (The value for one of the HTTP headers is not in the correct format.)
ErrorCode: InvalidHeaderValue

Additional Information:
HeaderName: content-length
HeaderValue: 0

Content:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Error>
  <Code>InvalidHeaderValue</Code>
  <Message>The value for one of the HTTP headers is not in the correct format.
RequestId:9d7754b2-4e3d-4256-97f8-f77b43ab3756
Time:2023-12-13T19:36:11.536Z</Message>
  <HeaderName>content-length</HeaderName>
  <HeaderValue>0</HeaderValue>
</Error>

Headers:
Server: Azurite-Blob/3.26.0
x-ms-error-code: InvalidHeaderValue
x-ms-request-id: 9d7754b2-4e3d-4256-97f8-f77b43ab3756
Date: Wed, 13 Dec 2023 19:36:11 GMT
Connection: keep-alive
Keep-Alive: REDACTED
Transfer-Encoding: chunked
Content-Type: application/xml

Full class:

public class BlobQueueWriter : QueueWriterBase
{
    private BlobServiceClient ServiceClient { get; }
    private string ContainerName { get; }
    private string BlobName { get; }
    private BlobContainerClient Container { get; set; }
    private AppendBlobClient AppendBlob { get; set; }

    /// <summary>
    /// Create the object. This will prepare to write to BLOB storage
    /// </summary>
    /// <param name="azureBlobConnectionString">The Azure connection string.</param>
    /// <param name="path">The full pathname of the BLOB to write to. Includes the container.</param>
    /// <param name="options">The options for this logger.</param>
    public BlobQueueWriter(string? azureBlobConnectionString, string path, BlobLoggerOptions options) : base(options)
    {
        ServiceClient = new BlobServiceClient(azureBlobConnectionString);
        (ContainerName, BlobName) = path.SplitBlobFilename();
        Container = ServiceClient.GetBlobContainerClient(ContainerName);
        AppendBlob = Container.GetAppendBlobClient(BlobName);

        // the ILoggerProvider starts everything with a constructor call - so no async
        Container.CreateIfNotExists();

        // create the blob if it does not exist
        if (!AppendBlob.Exists())
            AppendBlob.CreateIfNotExists();
    }

    private static readonly byte[] CrLf = "\r\n"u8.ToArray();

    /// <inheritdoc />
    public override void WriteLine(List<string> messages)
    {
        try
        {
            using (var stream = new MemoryStream())
            {
                foreach (var message in messages)
                {
                    var bytes = Encoding.UTF8.GetBytes(message);
                    stream.Write(bytes, 0, bytes.Length);
                    stream.Write(CrLf);
                }

                stream.Position = 0;
                AppendBlob.AppendBlock(stream);
            }

        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine($"BlobQueueWriter.WriteLine() threw exception {ex}");
            Container = ServiceClient.GetBlobContainerClient(ContainerName);
            AppendBlob = Container.GetAppendBlobClient(BlobName);
        }
    }
}

Any idea what's wrong?


Solution

  • It could happen when messages is empty. Try adding a check for it and skip invoking the AppendBlob.AppendBlock(stream); in that condition.

    /// <inheritdoc />
    public override void WriteLine(List<string> messages)
    {
        try
        {
            if (messages?.Any() != true)
            {
                return;
            }
    
            using (var stream = new MemoryStream())
            {
                foreach (var message in messages)
                {
                    var bytes = Encoding.UTF8.GetBytes(message);
                    stream.Write(bytes, 0, bytes.Length);
                    stream.Write(CrLf);
                }
    
                stream.Position = 0;
                AppendBlob.AppendBlock(stream);
            }
    
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine($"BlobQueueWriter.WriteLine() threw exception {ex}");
            Container = ServiceClient.GetBlobContainerClient(ContainerName);
            AppendBlob = Container.GetAppendBlobClient(BlobName);
        }
    }