如何解决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'));
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。