how to get name of file that trigger lambda from s3 in java

Viewed 652

Tell me how can I get the file name that trigger lambda from s3 inside lambda code in java? My task is to copy a file from s3 which goes there to my sftp server use lambda. In order for me not to copy every time all the files from the folder, I need to find out which file calling lambda.

P.S. Thank you in advance

1 Answers

Sample event that is sent to Lambda is here.

We can use java-events jar, will give us S3Event class with exact schema.

We need to loop on records and find s3.object.key:

package com.test;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.S3Event;

public class MyHandler implements RequestHandler<S3Event, String> {
    @Override
    public String handleRequest(S3Event request, Context context) {
        request.getRecords().forEach(record -> {
            System.out.println(record.getS3().getObject().getKey());
        });
        return "success";
    }
}
Related