AWS无服务器功能未响应映像 [英] AWS serverless function is not responding with an image
问题描述
我正在尝试让AWS API Gateway用图像进行响应.我的无服务器Lambda代码如下
I'm trying to have AWS API Gateway respond back with an image. My Serverless Lambda code is the following
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
返回的图像是一个黑框.
The image that is coming back is a black box.
推荐答案
在API Gateway中处理二进制文件总是很麻烦的.不过,我设法使它起作用.
Dealing with binaries in API Gateway is always a hassle. I have managed to make it work, though.
您需要做的就是告诉API Gateway您的响应是使用base64编码的.
All you need to do is tell API Gateway that your response is encoded in base64.
这是一个可行的解决方案:
Here's a working solution:
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来管理,以使其正常工作.
The real problem I see here, however, is that Express is managing the routes for you, therefore I don't think you can intercept API GW's response to add the field 'isBase64Encoded', so I am afraid you'll have to let this API be managed by API Gateway instead of Express in order to make it work properly.
此外,Jimp提供了一个getBufferAsync
方法,该方法返回一个Promise,因此您只需在它上await
即可使代码稍微简单些.
Also, Jimp offers a getBufferAsync
method which returns a promise, so you can just await
on it to make the code slightly simpler.
希望有帮助!
EDIT :
EDIT:
我仍在尝试使其与Express一起使用,因此我发现了这一点: https://github.com/awslabs/aws-serverless-express/issues/99#issuecomment-332169739
I was still trying to make it work with Express, so I found this: https://github.com/awslabs/aws-serverless-express/issues/99#issuecomment-332169739
我必须承认我没有进行测试,但是如果您确实需要Express来为您处理路线,这可能会起作用.
I must admit I did not test, but it may work if you really need to have Express handling the routes for you.
这篇关于AWS无服务器功能未响应映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!