AWS Lambda and S3 integration using AWS Java Sdk

Viewed 1741

I am trying to develop a AWS lambda function which is triggered when a file shows up in a specific s3 bucket. I am trying to follow the examples from AWS Lambda documentation, using aws-java-sdk-lambda 1.11.192, aws-java-sdk-s3 1.11.192. But, unfortunately the these examples use RequestHandler which is deprecated in the latest version of the jar.

My code is similar to this example

package example;

import java.net.URLDecoder;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.S3Event;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord;

public class S3GetTextBody implements RequestHandler<S3Event, String> {

public String handleRequest(S3Event s3event, Context context) {
    try {
        S3EventNotificationRecord record = s3event.getRecords().get(0);

        // Retrieve the bucket & key for the uploaded S3 object that
        // caused this Lambda function to be triggered
        String bkt = record.getS3().getBucket().getName();
        String key = record.getS3().getObject().getKey().replace('+', ' ');
        key = URLDecoder.decode(key, "UTF-8");

        // Read the source file as text
        AmazonS3 s3Client = new AmazonS3Client();
        String body = s3Client.getObjectAsString(bkt, key);
        System.out.println("Body: " + body);
        return "ok";
    } catch (Exception e) {
        System.err.println("Exception: " + e);
        return "error";
    }

  }
}

The current version of the aws sdk for lambda doesn't contain -

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

What are my alternatives? How can I achieve similar functionality using the newer versions of their sdk.

1 Answers
Related