简体   繁体   English

使用 Azure 存储节点 SDK 创建队列消息时,队列触发 Azure 函数抛出异常

[英]Queue triggered Azure function throws exception when Queue message is created with the Azure Storage Node SDK

I use the Azure Storage Node SDK to add a message to an Azure Storage Queue.我使用Azure 存储节点 SDK向 Azure 存储队列添加消息。 Following the official exampel, my code looks like this:按照官方示例,我的代码如下所示:

const AzureStorage = require('azure-storage');
const queueService = AzureStorage.createQueueService();

queueService.createMessage('taskqueue', 'Hello world!', function (error) {
  if (!error) {
    // Message inserted
  }
});

This adds a message to the taskqueue queue, which in turn triggers a Queue triggered Azure Function built with Node.这增加了一个消息发送到taskqueue的队列,这反过来触发队列触发与节点内置天青功能。 When the Azure Function receives the message, it throws the following exception:当 Azure Function 收到消息时,它会抛出以下异常:

Exception while executing function: Functions.Function2. 
Microsoft.Azure.WebJobs.Host: Exception binding parameter 'queuedMessage'. 
mscorlib: 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.

After a fair amount of Googling, when I couldn't find anything in the official documentation, I found this excellent post .经过大量的谷歌搜索,当我在官方文档中找不到任何内容时,我找到了这篇优秀的帖子

Apparently there is an inconsistency between how messages are encoded (by default) via Azure Storage Node SDK and how they are decoded via Queue triggered Node functions.显然,消息是如何通过 Azure 存储节点 SDK 进行编码的(默认情况下)与如何通过队列触发节点函数进行解码之间存在不一致。 According to above referenced post, the Azure Storage SDK defaults to using the TextXmlQueueMessageEncoder , while the Azure Function expects the message to be encoded with the TextBase64QueueMessageEncoder .根据以上引用的文章,该Azure存储SDK默认使用TextXmlQueueMessageEncoder ,而天青功能预计消息到与被编码TextBase64QueueMessageEncoder

New @azure/storage-queue lib新的@azure/storage-queue

The inconsistency remain in the new queue library as well.不一致也保留在新的队列库中。 In this library I haven't been able to find a built-in way to switch the encoder, but manually base64-enconding the string does the trick:在这个库中,我无法找到切换编码器的内置方法,但是手动对字符串进行 base64 编码可以解决问题:

const { QueueServiceClient } = require("@azure/storage-queue")

const base64Encode = (str) => Buffer.from(str).toString('base64')

const queueServiceClient = new QueueServiceClient(...)
queueServiceClient.getQueueClient('myqueue').sendMessage(base64Encode('Hello World!'))

Old azure-storage lib旧的azure-storage

In the old library, manually overriding the default encoder solves the problem.在旧库中,手动覆盖默认编码器可以解决问题。

const AzureStorage = require('azure-storage');
const QueueMessageEncoder = AzureStorage.QueueMessageEncoder;

const queueService = AzureStorage.createQueueService();
queueService.messageEncoder = new QueueMessageEncoder.TextBase64QueueMessageEncoder();
queueService.createMessage('taskqueue', 'Hello world!', function(error) {
  if (!error) {
    // Message inserted
  }
});

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

相关问题 将 JSON 消息发送到 Azure 队列存储 - Send JSON message to Azure Queue storage 反序列化从 node.js (azure sdk) 发送的 Azure ServiceBus 队列消息时出错 - Error while deserializing Azure ServiceBus Queue message sent from node.js (azure sdk) Azure存储队列-空触发 - Azure Storage Queue - Trigger on empty 如何使用 Node.js 监听 azure 存储队列? - How to listen to azure storage queue with Node.js? Azure Function v2 在 NodeJS (index.js) 中使用 ServiceBusQueueTrigger - 如果出现异常,如何将消息发送到死信队列 - Azure Function v2 with ServiceBusQueueTrigger in NodeJS (index.js) - How to send message to Deadletter Queue if there is exception 从数组推送到 Azure 队列覆盖消息?[node.JS] - Push to Azure queue overwriting message from Array?[node.JS] 当节点服务器抛出异常时,事件队列会发生什么 - What happens to event queue when a node server throws exception Azure:如果队列中有消息,如何触发nodejs webjob? - Azure: how to trigger a nodejs webjob when there is a message in the queue? 将数千条消息添加到Azure存储队列 - add thousands of messages to an Azure Storage Queue 更新后无法删除Azure存储队列消息(找不到消息) - Can't delete azure storage queue message after it has been updated (Message not found)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM