如何解决如何使用Netlify Lambda代理图像
我的最终目标是通过我的网站从凭据安全的API(someServer.com
)中获取图片。
由于我想在后端保留凭据,因此触发了呼叫JavaScript Netlify Function。此功能可以访问包含凭据的密钥process.env.CREDENTIALS
。
我的函数可以检索图像,但是我不知道如何将其转发到前端。
我执行的代码:
const fetch = require("node-fetch").default;
exports.handler = (event,context,callback) => {
const url = "https://someServer.com/myImage";
fetch(url,{
headers: { Authorization: `Bearer ${process.env.CREDENTIALS}` },})
.then((response) => {
callback(null,{
statusCode: 200,body: response.blob(),});
})
.catch((e) => console.log(`Catching error: ${e}`));
};
返回
Fetching image
Response with status 200 in 120 ms.
Catching error: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string or Buffer. Received type object
将图像返回到前端的正确方法是什么?
里程碑01
我设法通过
const fetch = require("node-fetch").default;
exports.handler = (event,{
headers: { Authorization: `Bearer ${process.env.NINOX_API_KEY_SLANZ}` },})
.then((response) => response.blob())
.then((blob) => blob.arrayBuffer())
.then((buffer) => {
callback(null,body: new Buffer.from(buffer),});
})
.catch((e) => console.log(`Catching error: ${e}`));
};
这在netlify-lambda
上本地工作,但是在AWS上不工作。 biphobe“ Netlify的函数,就像AWS Lambda的函数一样,需要主体为字符串。”
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。