简体   繁体   English

如何从 Azure 存储队列的消息文本字段中读取 JSON 字符串(使用 Azure 函数)?

[英]How do I read a JSON string from the Message Text field in an Azure Storage queue (using an Azure function)?

I am trying to bind an Azure function to a queue and get the following error:我正在尝试将 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输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非法字符

This is a sample of what the queue looks like:这是队列的示例:

在此处输入图像描述

The following is the code being used to attempt to read the queue:以下是用于尝试读取队列的代码:

Code:代码:

namespace GetInvoicePayload
{
    public static class LoadtoDB
    {
        [FunctionName("LoadtoDB")]
        public static void Run([QueueTrigger("incoming", Connection = "ConnectionStrings")] string myQueueItem, ILogger log)
        {

            //string json = JsonConvert.SerializeObject(myQueueItem);

                //byte[] json = Encoding.ASCII.GetBytes(myQueueItem);
                File.WriteAllText("C:\\dspl\\WriteLines.txt", myQueueItem);


            log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
        }
    }
}

the JSON string is being serialised through the following code snippet (serialising an ExpandoObject) JSON 字符串正在通过以下代码片段进行序列化(序列化 ExpandoObject)

string queueMessage = JsonConvert.SerializeObject(queueMessageE);

            QueueClient queueClient = new QueueClient(connectionString, "incoming");

            if (queueClient.Exists())
            {
                // Send a message to the queues
                queueClient.SendMessage(queueMessage);
            }

EDIT: I have added the following based on the comment below but still get the same error message.编辑:我根据下面的评论添加了以下内容,但仍然收到相同的错误消息。 I have checked and the queue data is correct.我已经检查并且队列数据是正确的。

在此处输入图像描述

This is the postman payload:这是 postman 有效载荷:

在此处输入图像描述

and what the queue receives (now in the poison queue because of the error):以及队列接收的内容(由于错误,现在在毒队列中):

在此处输入图像描述

The reason you are getting this error is because Queue Triggered Azure Functions expect the message body to be bas64 encoded.您收到此错误的原因是因为队列触发 Azure 函数期望消息正文为 bas64 编码。

From this link :从这个link

Functions expect a base64 encoded string.函数需要一个 base64 编码字符串。 Any adjustments to the encoding type (in order to prepare data as a base64 encoded string) need to be implemented in the calling service.对编码类型的任何调整(为了将数据准备为 base64 编码字符串)都需要在调用服务中实现。

What you would need to do is send the message bas64 encoded to the queue and then decode it back in your function code.您需要做的是将消息 bas64 编码发送到队列,然后将其解码回您的 function 代码中。

Something like:就像是:

queueClient.SendMessage(Convert.ToBase64String(Encoding.UTF8.GetBytes(queueMessage)));

and

string messageBody = Encoding.UTF8.GetString(Convert.FromBase64String(myQueueItem));

I have figured this one out:我想出了这个:

Mistake 1: I had accidentally used the Service Bus function template instead of the Queue triggered one.错误 1:我不小心使用了服务总线 function 模板而不是队列触发的模板。 This helped solve the issue of accessing the message in the queue.这有助于解决访问队列中的消息的问题。 Tested the boilerplate method and the following code line returned the message:测试了样板方法,以下代码行返回了消息:

log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

Mistake 2: Interestingly the reverse encoding of the message happens through the call to the queue itself.错误 2:有趣的是,消息的反向编码是通过调用队列本身发生的。 There is no need to convert the Base64 string back (it actually throws and error).无需将 Base64 字符串转换回来(它实际上会抛出错误)。 The string that is returned is already decoded.返回的字符串已经解码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM