Azure Function JS:从Blob获取图像并转换为base64

问题描述

我正在开发一个运行在js节点上的azure函数。此函数应从Blob获取图像并将其转换为base64字符串。问题是当我调用toString('base64')时,我的函数挂起了(看起来像是无限循环)。我该如何解决这个问题以及什么会导致问题?

function.json

{
  "bindings": [
    {
      "authLevel": "function","type": "httpTrigger","direction": "in","name": "req","methods": [
        "post"
      ],"route": "my_func"
    },{
      "type": "http","direction": "out","name": "res"
    },{
      "name": "templateImage","type": "blob","path": "assets/{templateImage}.jpg","dataType": "binary","connection": "Storage","direction": "in"
    }
  ]
}

index.js

module.exports = async function (context,req) {
   let templateImage = context.bindings.templateImage; // This is Buffer.
   console.log(templateImage); // I can log it. Will see something like this: <Buffer ff d8 ff e0 00...
   console.log(templateImage.toString('base64')); // I want to get base64,but after calling it my function is stuck.
    ...
}

更新: 我认为值得一提的是,该功能仅在本地开发中挂起。在azure门户中进行转换,但结果看起来很奇怪:����\ u0000 \ u0010JFIF \ u0000 \ u0001 \ u0002 \ u0001 \ u0001,\ u0001,\ u0000 \u0000��\ u0000,Photoshop。虽然应该是这样的:data:image / jpeg; base64,/ 9j / 4AAQ ...

解决方法

我在自己的网站上进行了测试,效果很好。您可以参考以下代码进行故障排除。

在index.js中,注意在参数中添加myinput in

module.exports = async function (context,req,myinput) {
    context.log('JavaScript HTTP trigger function processed a request.');
    let me = context.bindings.myinput;
    console.log(me);
    console.log(me.toString('base64'));
}

本地主机中的快照: enter image description here

门户中的快照: enter image description here