【问题标题】:AWS serverless function is not responding with an imageAWS 无服务器功能没有响应图像
【发布时间】:2019-07-17 08:15:45
【问题描述】:

我正在尝试让 AWS API Gateway 回复一张图片。我的无服务器 Lambda 代码如下

const express = require('express');
const serverless = require('serverless-http');
const bodyParser = require('body-parser');
const request = require('request');
const fetch = require('node-fetch')
var Jimp = require('jimp');
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))

app.get('/image/:id', async(req, res) => {
    const id = req.params.id;

    var imgUrl = 'https://developer.salesforce.com/forums/profilephoto/729F00000005O41/T';
    let options = {};

    const image = await Jimp.read(imgUrl);
    image.getBuffer(Jimp.MIME_JPEG, (err, buffer) => {
        res.set('content-type', 'image/jpeg');
        res.send(buffer.toString('base64'));
    });
});
// wrap express app instance with serverless http function
module.exports.handler = serverless(app)

serverless.yml


provider:
  name: aws
  runtime: nodejs8.10
  stage: dev
  region: us-east-1
  memorySize: 512

custom:
  apigwBinary:
    types:           #list of mime-types
      - 'image/jpg'
      - 'image/jpeg'
      - 'image/png'
functions:
  avatarFunc:
    handler: index.handler
    events:
      - http:
          path: image/{id}
          method: get
          contentHandling: CONVERT_TO_BINARY

plugins:
  - serverless-offline
  - serverless-apigw-binary

返回的图像是一个黑盒子。

【问题讨论】:

    标签: node.js amazon-web-services serverless-framework aws-serverless


    【解决方案1】:

    在 API Gateway 中处理二进制文件总是很麻烦。不过,我已经设法让它工作了。

    您需要做的就是告诉 API Gateway 您的响应是用 base64 编码的。

    这是一个可行的解决方案:

    module.exports.hello = async (event, context) => {
      const imgUrl = 'https://developer.salesforce.com/forums/profilephoto/729F00000005O41/T';
      const image = await jimp.read(imgUrl);
      const buffer = await image.getBufferAsync(jimp.MIME_JPEG);
      return {
        statusCode: 200,
        headers: {
          'content-type': 'image/jpeg'
        },
        body: buffer.toString('base64'),
        isBase64Encoded: true
      };
    
    };
    

    然而,我在这里看到的真正问题是 Express 正在为您管理路由,因此我认为您无法拦截 API GW 的响应以添加字段“isBase64Encoded”,所以我担心您必须让此 API 由 API Gateway 而不是 Express 管理,才能使其正常工作。

    此外,Jimp 提供了一个 getBufferAsync 方法,该方法返回一个承诺,因此您可以在其上使用 await 以使代码稍微简单一些。

    希望对你有帮助!

    编辑

    我仍在尝试让它与 Express 一起使用,所以我发现了这个:https://github.com/awslabs/aws-serverless-express/issues/99#issuecomment-332169739

    我必须承认我没有测试,但如果您确实需要 Express 为您处理路线,它可能会起作用。

    【讨论】:

      【解决方案2】:

      好的。我刚刚经历了这个,并认为我会分享解决方案。该问题与无服务器和 AWS 之间的不匹配有关。所以我们要把它们放在同一个页面上。

      FIRST-> 无服务器配置

      const binaryMimeTypes = {binary: [
        'image/*',
        'image/jpeg',
        'image/png',
        'image/svg+xml',
      ]};
      
      module.exports.server = sls(app, binaryMimeTypes) 
      

      这会将 Serverless 配置为将关联的 Mime 类型作为 Base64 服务。

      第二个-> AWS 配置

      在 AWS API 中选择网关,然后选择设置。向下滚动并添加以下二进制类型:

      就是这样!它现在应该可以工作了!

      【讨论】:

      • 我不知道Buffer.fromres.sendres.end有多少修订版,我在找到这个之前我已经看过了。 AGW/服务器级别!啊。谢谢!
      猜你喜欢
      • 2018-06-23
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      • 2011-08-22
      • 2017-01-09
      • 2017-01-04
      相关资源
      最近更新 更多