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 ( Most importantly, you must have a registered AWS account. For more information on setting up the AWS account, follow this link (


package sampleProgram;
import java.util.Scanner;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;

// 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 
                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();
	        	   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();	        
        	                } catch (IOException e) {
		   //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.
                  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.
            	return "error";

Step 3:
To execute above lambda function, follow the steps mentioned here (



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


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 *