我最近开始使用 azure 函数将 base64 字符串转换为图像并将该图像存储在 blob 中。 但我没有找到任何关于如何将特定文件类型写入 blob 的信息,就像我通常在 Node 中使用 fs.writeFile() 所做的那样。
有人可以告诉我是否可以使用 Azure Functions 的正常“输出”来完成此操作? 目标是我可以将 base64 字符串转换为图像并将其保存在 blob 中。
module.exports = function (context, input) {
var image = input;
var bitmap = new Buffer(image, 'base64');
//base64 string to test: "data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
function decodeBase64Image(dataString) {
var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
response = {};
if (matches.length !== 3) {
return new Error('Invalid input string');
}
response.type = matches[1];
response.data = new Buffer(matches[2], 'base64');
return response;
}
var imageBuffer = decodeBase64Image(input);
//context.bindings.outputBlob = {'test2.jpg', imageBuffer.data};
context.done(null, imageBuffer);
};
我的functions.json看起来像这样:
{
"bindings": [
{
"type": "manualTrigger",
"direction": "in",
"name": "input"
},
{
"type": "blob",
"name": "outputBlob",
"path": "logo/{rand-guid}",
"connection": "npsmonitordev_STORAGE",
"direction": "out"
}
],
"disabled": false
}
最佳答案
是的,这是可能的。
在我的测试中,我只是将 outputBlob
设置为 imageBuffer.data
,如下所示:
context.bindings.outputBlob = imageBuffer.data;
context.done();
在我的function.json
中我有这个:
{
"bindings": [
//...
{
"type": "blob",
"name": "outputBlob",
"path": "outcontainer/{rand-guid}.png",
"connection": "aaronchstorage_STORAGE",
"direction": "out"
}
],
"disabled": false
}
这会将缓冲区数据作为 bolb 文件保存到 Azure 存储。
关于javascript - Azure 函数 Node.js : write image to blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45002001/