[英]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.