AWS API Gateway和Lambda返回映像 [英] AWS API Gateway and Lambda to return image

查看:207
本文介绍了AWS API Gateway和Lambda返回映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有这个HTML:

<img src="http://example.com/pic"/>

我想做的是将example.com/pic映射到AWS API Gateway终端节点.

What I would like to do is have example.com/pic map to an AWS API Gateway endpoint.

然后该端点将调用lambda函数.

That endpoint would then call a lambda function.

该lambda函数将从s3存储桶中读取随机图像并将其返回.

That lambda function would read a random image from an s3 bucket and return it.

所以我的目标是使用标准HTML图像标签并以s3存储桶中的图像为最终结果,但要通过lambda中的某些决策代码来确定要返回的图像.

So my aim is to use a STANDARD HTML image tag and end up with an image from an s3 bucket but going via some decision code in the lambda to decide the image to return.

我知道您可以使用s3直接提供静态内容(因此,lambda可以决定要显示什么图像).我也知道我可以在lambda中做一些事情,例如b64编码响应,然后在客户端上处理它,但是我的目标是使用标准的HTML IMG标签.

I know you can use s3 to serve static content directly (hence the lambda to make the decision about what image). I also know I could do stuff in the lambda like b64 encode the response and then handle it on the client but I am aiming to use the standard HTML IMG tag.

这可能吗?

我尝试将ResponseStreamHandler(Java SDK)用于lambda并返回图像的字节数组,并且还添加了API网关配置以不将输出映射到JSON,但是似乎没有任何作用!

I've tried using the ResponseStreamHandler (Java SDK) for the lambda and returning the byte array of the image and also added the API gateway config to not map the output to JSON, but nothing seems to work!

推荐答案

AWS似乎简化了此过程,因此许多答案已经过时和/或过于复杂.

It seems AWS has simplified this process, so that many answers are outdated and/or overly complicated.

截至2018年6月,这就是我让Lambda通过API网关返回图像的方式:

This is how I got Lambda to return an image through the API Gateway, as of June 2018:

1)在API网关中,为您的API启用Use Lambda Proxy integration. (此设置位于将类型设置为Lambda的集成请求"部分.)

1) In API Gateway, enable Use Lambda Proxy integration for your API. (This setting is located on the Integration Request section, where you had set the type to Lambda.)

2)在API网关中,选择您的API,然后单击Settings.在Binary Media Types中添加*/*. (注意:我尝试仅添加'image/jpeg',但这似乎需要*/*才能使所有这些正常工作)

2) In API Gateway, select your API and click Settings. In Binary Media Types add */*. (Note: I tried adding simply 'image/jpeg', but it seems to require */* to get all of this to work)

3)请确保部署您的API,否则您的更改将无法生效. (在API网关中,选择您的API,然后选择操作">部署API".

3) Be sure to deploy your API, otherwise your changes will not be live. (In API Gateway, select your API, then Actions > Deploy API).

4)在您的Lambda代码中,以Base64编码返回图像(此示例为C#代码):

4) In your Lambda code, return your image in Base64 encoding (this example is C# code):

    // set the Content-type header
    // set to whichever image type you're returning
    var headersDic = new Dictionary<string, string>();
    headersDic.Add("Content-type", "image/jpeg");

    // return the response object that APIGateway requires
    return new APIGatewayProxyResponse
    {
        StatusCode = 200,
        Headers = headersDic,
        // return the image in Base64 encoding
        Body = Convert.ToBase64String(...your image data...),
        IsBase64Encoded = true
    };

完成.

如果您已将API设置为不需要身份验证,只需在浏览器中键入API链接,它将显示图像.或将API链接放入IMG标记.例如<img src="https://asdf.execute-api.us-east-1.amazonaws.com/live/myapi" />

If you've setup your API to not require authentication, simply type your API link into your browser, and it will display the image. Or put the API link into an IMG tag. e.g. <img src="https://asdf.execute-api.us-east-1.amazonaws.com/live/myapi" />

注意:即使在步骤2中将Binary Media Types设置为*/*,如果Lambda返回的内容,API Gateway仍会返回文本.

Note: Even though in step 2 you set the Binary Media Types to */*, API Gateway will still return text if that is what your Lambda is returning.

这篇关于AWS API Gateway和Lambda返回映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆