0
votes

I'm developing an azure function, running in node js. This function should get an image from the blob and transform it to base64 string. The problem is when I'm calling toString('base64') my function hangs (looks like some infinite loop). How can I fix this and what can cause the problem?

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.
    ...
}

UPDATE: I think it's worth to mention, that function hangs only in local development. In azure portal it does transformation, but result looks strange: ����\u0000\u0010JFIF\u0000\u0001\u0002\u0001\u0001,\u0001,\u0000\u0000��\u0000,Photoshop . While it should be something like this: data:image/jpeg;base64,/9j/4AAQ...

1

1 Answers

0
votes

I test in my site and it works very well. You could refer to the following code to troubleshoot.

In index.js, pay attention to add myinput int the parameter:

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'));
}

The snapshot in localhost: enter image description here

The snapshot in portal: enter image description here