简体   繁体   English

使用参数从ruby SDK调用Java中的AWS Lambda

[英]Calling AWS Lambda in Java from ruby SDK with arguments

I'm trying to call a AWS Lambda function using Ruby SDK v3 我正在尝试使用Ruby SDK v3调用AWS Lambda函数

    Aws::Lambda::Client.new.invoke_async(function_name: ENV["FUNCTION_NAME"],
                                         invoke_args: { mas: 'que caralho' }.to_json)

It gets called as expected, but if I print the args: 它按预期方式被调用,但是如果我打印args:

    public void handleRequest(InputStream input, OutputStream output, Context context) {
        String input_string = String.valueOf(input.read());
        input.close();
        logger.info("Received input: " + input_string);
        ...

They always come as "123". 他们总是以“ 123”出现。 There's no 123 in my call D: 我的通话D中没有123:

I also came across the payload argument but it doesn't matter what I put there, it complains it is unexpected 我也遇到了payload参数,但是不管放在哪里都没关系,它抱怨这是意外的

ArgumentError: unexpected value at params[:payload]

Any help, please? 有什么帮助吗?

The input.read() command returns one character at a time . input.read()命令一次返回一个字符

Here is sample code from Example: Using Stream for Handler Input/Output (Java) - AWS Lambda : 这是示例:将流用于处理程序输入/输出(Java)-AWS Lambda的示例代码:

package example;

import java.io.InputStream;
import java.io.OutputStream;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.amazonaws.services.lambda.runtime.Context; 

public class Hello implements RequestStreamHandler{
    public void handler(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        int letter;
        while((letter = inputStream.read()) != -1)
        {
            outputStream.write(Character.toUpperCase(letter));
        }
    }
}

Therefore, your code is only reading one character, and it is coming through as an integer. 因此,您的代码仅读取一个字符,并且它作为整数出现。 123 in ascii is the { character, which is the beginning of the JSON being passed to your function. ascii中的123{字符,它是传递给您的函数的JSON的开头。

So, you will need to keep reading until it returns -1 . 因此,您将需要继续阅读,直到返回-1为止。

Alternatively, you could use the normal method definition, taken from AWS Lambda Function Handler in Java - AWS Lambda : 或者,您可以使用从Java中的AWS Lambda函数处理程序获取的常规方法定义-AWS Lambda

outputType handler-name(inputType input, Context context) {
   ...
}

This will let you read your input as one string , rather than having to loop through the stream. 这样一来,您就可以将输入作为一个字符串读取 ,而不必遍历流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM