将JSON文件通过API Gateway传递给Java AWS Lambda函数。

huangapple go评论108阅读模式
英文:

Pass JSON file to a Java AWS Lambda Function via API Gateway

问题

我正在测试AWS Lambda函数和API Gateway,并目前,我不能自己回答以下问题:
如何最佳实践地解析AWS API Gateway的请求中的JSON主体以传递给Lambda函数?

我编写了几个请求处理程序,它们使用字符串映射甚至默认的Java类来解析JSON输入。当我进行测试并将JSON定义传递给我的Lambda函数时,这种方法效果相当不错。但当我使用curl时,JSON数据当然位于请求正文中,而我的函数未提取它。所以我在想,通过Java方法解析请求主体中的JSON数据的最佳方法是什么?

是否有一种方法可以只是通过AWS API Gateway传递主体?还是我必须在我的Java方法中使用inputStream并自己提取JSON数据?

提前致谢!

英文:

I am testing a bit with AWS lambda functions and API Gateway and by now, I can not answer the following question to myself:
What is the best practice to parse a JSON Body of a Request via an AWS API Gateway to a Lambda Function?

I have written several Request Handlers, that parse a JSON Input using a map of String or even a default Java Class. This works quiet well when testing it and just passing the JSON definition to my Lambda function. But when it comes to the part, where I am using curl, the JSON data is, of course, in the request body and my Function is not extracting it. So I am wondering, what is the best way to parse JSON data that is in a Request Body via a Java Method?

Is there a way to just pass the body through the AWS API Gateway? Or do I just have to use an inputStream in my Java Method and extract the JSON data on my own?

Thanks in advance!

答案1

得分: 1

For this use case, try using Postman and configure it to pass JSON to the API Gateway endpoint. The API Gateway endpoint passes the JSON to the Lambda function. So in my example here, I have configured an API Gateway endpoint that accepts a PUT request and accepts JSON that specifies a file name.

这个用例中,尝试使用Postman并配置它以将JSON传递给API Gateway端点。API Gateway端点将JSON传递给Lambda函数。因此,在我的示例中,我已配置了一个API Gateway端点,它接受PUT请求并接受指定文件名的JSON。

The Lambda function generates a presigned URL and returns it, as shown at the bottom of the screenshot.

Lambda函数生成一个预签名URL并返回它,如屏幕截图底部所示。

See Postman sending the request:

查看Postman发送请求:

将JSON文件通过API Gateway传递给Java AWS Lambda函数。

The Lambda function gets the value like this within the Lambda handler. No need for input streams or anything like that. Notice the string value you specify within event.get() matches the value in JSON.

Lambda函数在Lambda处理程序中以这种方式获取值。无需输入流或类似的内容。注意你在**event.get()**中指定的字符串值与JSON中的值匹配。

public class UploadHandler implements RequestHandler<Map<String, String>, String> {

    @Override
    public String handleRequest(Map<String, String> event, Context context) {
        LambdaLogger logger = context.getLogger();
        String fileName = event.get("filename");
    
    ...

Of course, you have to set up your API gateway endpoint to use that specific Lambda function. Once you do, you can make requests, pass the JSON, and the Lambda function can get the value(s).

当然,你必须设置你的API Gateway端点以使用特定的Lambda函数。一旦你这样做,你就可以发送请求,传递JSON,Lambda函数就可以获取值。

英文:

For this use case, try using Postman and configure it to pass JSON to the API Gateway endpoint. The API Gateway endpoint passes the JSON to the Lambda function. So in my example here, I have configured an API Gateway endpoint that accepts a PUT request and accepts JSON that specifies a file name.

The Lambda function generates a presigned URL and returns it, as shown at bottom of screen shot.

See Postman sending the request:

将JSON文件通过API Gateway传递给Java AWS Lambda函数。

The Lambda function gets the value like this within the Lambda handler. No need for input streams or anything like that. Notice the string value you specify within event.get() matches the value in JSON.

public class UploadHandler implements RequestHandler&lt;Map&lt;String, String&gt;, String&gt; {

    @Override
    public String handleRequest(Map&lt;String, String&gt; event, Context context) {
        LambdaLogger logger = context.getLogger();
        String fileName = event.get(&quot;filename&quot;);

...

Of course, you have to setup your API gateway endpoint to use that specific Lambda function. Once you do - you can make requests, pass the JSON and the Lambda function can get the value(s).

huangapple
  • 本文由 发表于 2023年3月4日 01:42:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定