Sending base 64 messages to an Azure Storage Queue for Azure Function

If you happen to get this exception on an Azure Function when you have an Azure Storage Queue triggered Azure Function:

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

image

Figure 1, base-64 message required for Azure Functions storage

You can read here that the Encoding is expected to be base64.  The document doesn’t contain a solution so here are a couple.

The first one you should consider is one which I implemented using version 12.7.0 of the Azure.Storage.Queues NuGet Package, here.  Also, I am using .NET 5 Console application to upload the messages.  Here is the code snippet.

private static QueueClient _queueClient;

_queueClient = new QueueClient(StorageQueueConnectionString, StorageQueueName, new QueueClientOptions
{
    MessageEncoding = QueueMessageEncoding.Base64
});

This will make sure the message is base 64.  If the message is not base 64, then you will get the message noted above.

Another method is by using the following syntax:

String message = Base64.getEncoder().encodeToString(messageText.getBytes())

In both cases, you send the message to the identified Azure Storage Account and queue using the send methods, similar to the following snippet.

await _queueClient.SendMessageAsync(message);

Then you would expect to get a nice successfully executed message.

image

Figure 2, a base 64 encoded message for Azure Storage Queue – Azure Function processing

Finally, from a C#, default perspective, I was able to successfully send my messages to the Azure Storage Queue and the Azure Function processed them without needing to set the MessageEncoding property.  That is the case because my messages were simple and base 64 complaint.  If you are reading this article, then you likely have a more complex scenario.