gpt4 book ai didi

azure - Azure函数的绑定(bind)可以让消息作为缓冲区传递还是使用特定编码传递

转载 作者:行者123 更新时间:2023-12-03 23:35:00 25 4
gpt4 key购买 nike

我使用 azure 函数将消息从 eventhub 保存到 azure 表。但我遇到了一些编码问题。

我的场景是一个进程将 ascii 编码缓冲区发送到 eventhub,然后 azure 函数将其保存到表中。
但是,azure 函数从 eventhub 获取参数变成了 UTF8 字符串。这会导致一些无效的 UTF8 字节丢失。

现在我怀疑是否可以通过以下两种方式解决问题:

  1. 绑定(bind)设置是否可以让触发器的参数从 eventhub 到 azure 函数成为缓冲区而不是字符串。这里我开始使用nodejs模板。
  2. 或者,绑定(bind)让对象能够以特定编码进行字符串化。然后我可以再次重新构建缓冲区。

或者还有其他更好的方法来解决这个问题吗?

最佳答案

绑定(bind)支持字节数组。这是一个 C# 示例:

using System;
using System.Text;

public static void Run(byte[] myEventHubMessage, TraceWriter log)
{
string s1 = Encoding.UTF8.GetString(myEventHubMessage);
log.Info($"C# Event Hub trigger function processed a message: {s1}");
}

对于 Node,将数据类型设置为二进制。示例 function.json

{
"bindings": [
{
"type": "httpTrigger",
"name": "req",
"direction": "in",
"dataType": "binary"
},
{
"type": "http",
"name": "res",
"direction": "out"
}
]
}

对应的index.js:

module.exports = function (context, req) {
var body = req.body;

context.log("TestResult:", {
isBuffer: Buffer.isBuffer(body),
length: body.length
});

context.res = {
status: 200,
body: "Success!"
};

context.done();
}

希望这有帮助!

关于azure - Azure函数的绑定(bind)可以让消息作为缓冲区传递还是使用特定编码传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40375655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com