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

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

问题描述

假设我有这个 HTML:

Say I have this HTML:

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

我想做的是将 example.com/pic 映射到 AWS API 网关端点.

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.

所以我的目标是使用 STANDARD 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.

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

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

1) 在 API Gateway 中,为您的 API 启用使用 Lambda 代理集成.(此设置位于集成请求部分,您已在其中将类型设置为 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 Gateway 中,选择您的 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 Gateway 中,选择您的 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天全站免登陆