我设法创建了一个独立的快递应用,该应用使用multer接收图像,然后向aws rekognition api发送请求,并尝试猜测它是哪个名人。
当我用serverless-http
包包装它以使其与serverless
框架兼容时,api响应表示该图像具有一个Invalid Image Encoding
。
基于登录传入缓冲区的结果(如底部所示),serverless-http
不会转换传入数据以使multer能够正确读取,但是我不确定为什么吗?
以下是代码和配置文件:
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);
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
我正在使用邮递员发送多部分表格请求:
<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... >
<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 ... >