Using AWS Lambda function in Java to communicate with AWS S3

|
| By Webner

How to Use AWS Lambda function in Java to communicate with AWS S3?

Reading, writing and uploading a text file to S3 using AWS Lambda function in Java

Following are the steps to write a sample Lambda function in Java to work with the files that are placed on Amazon S3 bucket. For this scenario, we will read a text file which is placed inside an S3 bucket. After that, we will write the content of that text file to a new file and upload it on S3 bucket.

Step 1:

Create a sample text file inside your S3 bucket and write some multiline text in it.

Example: (Data.txt)
This is the sample text which is
written as multiline statements.
This text will be read line by line
and written to a text file before uploading on S3.

Step 2:

Java code below reads the contents of the text file you want to read from S3 bucket, scans the file line-by-line and then writes it to another text file before uploading it to same or another S3 bucket using AWS Lambda function. You can use any Java IDE to write Lambda function. However, the prerequisite is that you need to install AWS toolkit in your IDE to work with AWS services. The complete process for doing this is described here (https://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/setup-install.html). Most importantly, you must have a registered AWS account. For more information on setting up the AWS account, follow this link (https://docs.aws.amazon.com/lambda/latest/dg/setup.html).

Program:

package sampleProgram;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
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.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;

// your class needs to implement RequestHandler interface to be a Lambda function
public class MyTask implements RequestHandler{ 	
    //this method is defined inside RequestHandler interface and needs to be implemented here
    public String handleRequest(S3Event s3event, Context context) {         
      	    //obtaining S3 bucket attributes
	    S3EventNotificationRecord record = s3event.getRecords().get(0);
	    String srcBucket = record.getS3().getBucket().getName();
	    String dstBucket = 	"destinationBucketName";	
	    String srcKey = record.getS3().getObject().getKey().replace('+', ' '); //srcKey is the file name to be read with absolute path 
                System.out.println("srcKey:"+srcKey);
                try {
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();         
S3Object s3Object = s3Client.getObject(new GetObjectRequest(
                    srcBucket, srcKey));	//get object file using source bucket and srcKey name
           	       	   InputStream objectData = s3Object.getObjectContent(); //get content of the file
		   String textToUpload = "";
		   Scanner scanner = new Scanner(objectData);	//scanning data line by line
	               while (scanner.hasNext()) {
        		         textToUpload += scanner.nextLine();
	               }
	               scanner.close();
	        	   String objectKey = "output.txt";	// create object key ie. file to be uploaded
	               ObjectMetadata meta = null;InputStream is = null;
	               try {
        		          byte[] bytes = textToUpload.getBytes("UTF-8");
        		          is = new ByteArrayInputStream(bytes);
        		
		          //set meta information about text to be uploaded
        		          meta = new ObjectMetadata();	        
        		          meta.setContentLength(bytes.length);     
          meta.setContentType("text/html");
     
        	                } catch (IOException e) {
        		          e.printStackTrace();
        	                }
		   //finally upload the file by specifying destination bucket name, file name, input stream having content to be uploaded along with meta information
        	              s3Client.putObject(dstBucket, objectKey, is, meta);
    		  System.out.println("File uploaded.");
    		  return "Success";
       	    }
              catch(AmazonServiceException e) {
                  // The call was transmitted successfully, but Amazon S3 couldn't process 
                  // it, so it returned an error response.
                  e.printStackTrace();
                  return "error";
             }
             catch(SdkClientException e) {
             	// Amazon S3 couldn't be contacted for a response, or the client  
            	// couldn't parse the response from Amazon S3.
            	e.printStackTrace();
            	return "error";
            }
    }
}

Step 3:
To execute above lambda function, follow the steps mentioned here (https://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/lambda-tutorial.html).

Output:-

AWS

After executing the function, you can check that output file has been created in the S3 bucket specified in above code.

output.txt:

This is the sample text which is written as multiline statements. This text will be read line by line and written to a text file before uploading on S3.

Leave a Reply

Your email address will not be published. Required fields are marked *