如何使用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 (将#修改为@)