在 AWS Lambda 函数中从 S3 获取对象并发送到 Api 网关 [英] Get object from S3 in AWS Lambda function and send to Api Gateway

查看:35
本文介绍了在 AWS Lambda 函数中从 S3 获取对象并发送到 Api 网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从存储桶中获取 .jpg 文件并将其发送回 api 网关.我相信我的设置是正确的,因为我看到了正在记录的内容.它可以很好地从 s3 中抓取文件,而 gm 是 graphicsmagick 库.不确定我是否正确使用它.

I am trying to get a .jpg file from a bucket and send it back to api gateway. I believe I have the setup correct as I see stuff being logged. It grabs the file from s3 fine, and gm is the graphicsmagick library. Not sure if I am using it right though.

在 lambda 函数中我这样做(很多代码来自 aws 示例):

In the lambda function I do this (alot of the code comes from the aws example):

async.waterfall([
    function download(next) {
        console.log(srcKey);
        console.log(srcBucket);
        // Download the image from S3 into a buffer.
        s3.getObject({
                Bucket: srcBucket,
                Key: srcKey
            },
            next);
        },
    function transform(response, next) {
        console.log(response);
        next(null, 'image/jpeg', gm(response.Body).quality(85));

    },

    function sendData(contentType, data, next){
        console.log(contentType);
        console.log(data);
        imageBuffer = data.sourceBuffer;
        context.succeed(imageBuffer);
    }
    ]
);

响应头的content-length:85948,看起来不对,因为原始文件只有36kb.有人知道我做错了什么吗?

The response header has content-length: 85948, which doesn't seem right because the original file is only 36kb. Anyone know what I'm doing wrong?

推荐答案

您可以轻松实现 Get Image <-> API Gateway <-> Lambda <-> S3 集成.

You can achieve Get Image <-> API Gateway <-> Lambda <-> S3 integration with ease.

在 lambda 中,而不是 json,返回图像的 base64 字符串表示(buffer.toString('base64')),强制 API Gateway 将字符串转换为二进制并添加特定的 Content-Type(这样您就不需要使用他们有限的二进制支持来强制您发送特定的 Accept 标头).

In lambda, instead of json, return base64 string representation of image (buffer.toString('base64')), force API Gateway to convert the string to binary and add specific Content-Type (so you don't need to use their limited binary support that enforce you to send a specific Accept header).

在 AWS 控制台中,转到 API Gateway,然后转到相关方法并更新设置:

In AWS console, go to API Gateway, then go to the relevant method and update the settings:

  • 集成请求

  • Integration Request

  • 取消选中:使用 Lambda 代理集成

方法响应

  • 添加响应 -> HTTP 状态:200
  • 添加标题:Content-Type

集成响应 -> Header Mapping -> Response header -> Content-Type

Integration Response -> Header Mapping -> Response header -> Content-Type

  • 映射值:'image/jpeg'(单引号)

从命令行,运行以下命令强制将字符串转换为二进制.首先,从 API Gateway 检索 rest-api-idresource-id.然后,在 CLI 中运行(将 rest-api-id 和 resource-id 替换为您自己的):

From command line, run the command below to force convert string to binary. First, retrieve the rest-api-id and the resource-id from API Gateway. Then, run in CLI (replace rest-api-id and resource-id with your own):

aws apigateway put-integration-response --rest-api-id <rest-api-id> --resource-id <resource-id> --http-method GET --status-code 200 --content-handling CONVERT_TO_BINARY

这篇关于在 AWS Lambda 函数中从 S3 获取对象并发送到 Api 网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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