Azure Functions Node.js:将图像写入 blob

Azure Functions Node.js: write image to blob

我最近开始使用 azure 函数将 base64 字符串转换为图像并将该图像存储在 blob 中。 但是我没有找到任何关于如何将特定文件类型写入 blob 的信息,就像我通常在 Node 中使用 fs.writeFile().

所做的那样

谁能告诉我是否可以使用普通的 "output" 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 存储。