使用无服务器

时间:2018-06-22 20:34:23

标签: node.js express multer serverless-framework amazon-rekognition

我设法创建了一个独立的快递应用,该应用使用multer接收图像,然后向aws rekognition api发送请求,并尝试猜测它是哪个名人。

当我用serverless-http包包装它以使其与serverless框架兼容时,api响应表示该图像具有一个Invalid Image Encoding

基于登录传入缓冲区的结果(如底部所示),serverless-http不会转换传入数据以使multer能够正确读取,但是我不确定为什么吗?

以下是代码和配置文件:

Http处理程序代码

require("dotenv").config();
const serverless = require("serverless-http");
const express = require("express");
const app = express();
const AWS = require("aws-sdk");
const rekognition = new AWS.Rekognition({ region: "eu-west-1" });
const imageParser = require("multer")().single("image");

app.post("/", imageParser, async (req, res) => {
  try {
    if (!req.file) throw new Error("Missing image");
    console.log(req.file.buffer) // Log output below
    const celeb = { Image: { Bytes: req.file.buffer } };
    const results = await rekognition.recognizeCelebrities(celeb).promise();
    res.json(results);
  } catch (e) {
    console.error(e);
    res.send(e.message);
  }
});

if (require.main === module) {
  // This will not get run if you use sls offline start
  app.listen(3000, err =>
    console.log(!err ? "Listening..." : "Failed " + err.message)
  );
}

module.exports.handler = serverless(app);

serverless.yml

plugins:
  - serverless-offline
service: guessCeleb

provider:
  name: aws
  runtime: nodejs8.10
  stage: dev
  region: eu-west-1
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "rekognition:*"
      Resource: "*"   

functions:
  guessImage:
    handler: index.handler
    events:
      - http:
          path: /
          method: post

我正在使用邮递员发送多部分表格请求:

Postman Request

无服务器+ Express

<Buffer c3 bf c3 98 c3 bf c3 a0 00 10 4a 46 49 46 00 01 02 00 00 01 00 0100 00 c3 bf c3 9b 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b... >

Just Express App(有效)

<Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 02 00 00 01 00 01 00 00 ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f ... >

0 个答案:

没有答案