简体   繁体   中英

Reading a file on aws s3 with a Java lambda

CONTEXT :

I am trying to read a csv file on AWS s3, compute its data and write the results on another csv in the same s3 bucket.

I tried the following code to create a class that would read my Main , with arguments stored in a text file named lambdaCmd.txt . Each time lambdaCmd.txt is modified, the lambda function is triggered and the content of lambdaCmd.txt is passed to the following class via the s3event parameter :

public class LambdaCmd implements RequestHandler<S3Event, Void>{

    static final Logger LOGGER = LogManager.getLogger(LambdaCmd.class);

    @Override
    public Void handleRequest(S3Event s3event, Context context) {

        //Getting my txt file's path
        S3EventNotification.S3EventNotificationRecord record = s3event.getRecords().get(0);
        String bkt = record.getS3().getBucket().getName();
        String key = record.getS3().getObject().getKey().replace('+', ' ');
        try {
            key = URLDecoder.decode(key, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            LOGGER.error(ex);
        }

        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();

        try {

            //Getting my text file as a String Stream
            InputStreamReader cmdStream =
                new InputStreamReader(s3Client.getObject(bkt, key).getObjectContent());
            BufferedReader br = new BufferedReader(cmdStream);

            //Parsing the command in cmdStream
            //...
            //The command is now "String[] cmdArray" to be passed to my main

            MyMain.main(cmdArray);
            //The main function reads and write from s3 with a similar use of s3Client

            br.close();

        } catch (IOException | IllegalArgumentException | NullPointerException ex) {
            LOGGER.error(ex);
        }

        return null;
    }
}

PROBLEM :

After logging some debug messages in this code, I figured out that the lambda function stops at this line :

        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();

Here is the CloudWatch log (I hide personal information with ########## ):

START RequestId: ########## Version: $LATEST
16:12:11.596 [main] DEBUG path.to.mylambdaclass.LambdaCmd - LambdaCmd started
16:12:11.616 [main] DEBUG path.to.mylambdaclass.LambdaCmd - Just before creating s3Client
END RequestId: ##########
REPORT RequestId: ##########    Duration: 12398.45 ms   Billed Duration: 12400 ms Memory Size: 128 MB   Max Memory Used: 67 MB

I also have a log message just after creating s3Client , but it is not printed in logs.

QUESTION :

Why is my lambda ending prematurely? Do I misuse some objects?

After adding the following try/catch wrapping all the code in handleRequest method (I was not catching Errors, this is why I did not see it) :

try {
    //My code above
} catch (Error | Exception e) {
    LOGGER.error(e);
}

I got a java.lang.OutOfMemoryError . I had set the lambda memory to 128MB since my code in local was working with less than that. But it seems that reading from s3 needs a bit more and now it works fine with 512MB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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