If statement not working in AWS Lamnda - S3Event code

0 votes

Hi,

I am using below  lambda function with S3Event.

But below if statement not working. If I execute below Lambda function by IntelliJ ide AWS plugin then its working. But if I trigger event in S3 bucket then this if statement is not working. And without if statement able to execute properly. Kindly suggest.

if(env.equalsIgnoreCase("stage")) {
package com.zackroppel.lambda.gradebook;


import com.amazonaws.services.lambda.AWSLambda;

import com.amazonaws.services.lambda.AWSLambdaClientBuilder;

import com.amazonaws.services.lambda.model.UpdateFunctionCodeRequest;

import com.amazonaws.services.lambda.model.UpdateFunctionCodeResult;

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.model.AmazonS3Exception;

import com.amazonaws.services.s3.model.GetObjectRequest;

import com.amazonaws.services.s3.model.S3Object;

import org.apache.commons.io.IOUtils;


import java.io.IOException;

import java.nio.ByteBuffer;


public class TestLambdaS3Event implements RequestHandler<S3Event, String> {


private AmazonS3 s3 = AmazonS3ClientBuilder.standard().build();

AWSLambda s3EventClient = AWSLambdaClientBuilder.standard().build();


public TestLambdaS3Event() {

}



@Override

public String handleRequest(S3Event event, Context context) {

context.getLogger().log("Received event: " + event);


// Get the object from the event and show its content type

String bucket = event.getRecords().get(0).getS3().getBucket().getName();

System.out.println("bucket##"+bucket);

String key = event.getRecords().get(0).getS3().getObject().getKey();

System.out.println("key##"+key);

try {

S3Object response = s3.getObject(new GetObjectRequest(bucket, key));

System.out.println("response##"+response);

String contentType = response.getObjectMetadata().getContentType();


byte[] byteArray = IOUtils.toByteArray(response.getObjectContent());

String env = bucket.substring(bucket.lastIndexOf("/")+1);

System.out.println("env@@"+ env);

if(env.equalsIgnoreCase("stage")) {

UpdateFunctionCodeRequest request = new UpdateFunctionCodeRequest().withFunctionName("sagemaker_stick_and_slip_prediction")

.withZipFile(ByteBuffer.wrap(byteArray));

System.out.println("request## " + request);

UpdateFunctionCodeResult responseUpdate = s3EventClient.updateFunctionCode(request);

System.out.println("responseUpdate## " + responseUpdate);

}

return contentType;

} catch (IOException | AmazonS3Exception e) {

e.printStackTrace();

System.out.println("Exception## "+ e.getMessage());

context.getLogger().log(String.format("Error getting object %s from bucket %s. Make sure they exist and"

+ " your bucket is in the same region as this function.", bucket, key));

return e.toString();

}


}


}
Apr 4, 2020 in Java by Abhishek
• 280 points
888 views
You are trying to use if statement in lambda function, right?
Hey, what's the error you are getting?

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Java

+1 vote
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,680 points
3,772 views
0 votes
1 answer

“Missing return statement” within if / for / while in Java

Putting a return statement in any of the ...READ MORE

answered Sep 27, 2018 in Java by code.reaper12
• 3,500 points
9,100 views
+2 votes
4 answers

Javac command not working in my windows 10

Check your javac path on Windows using Windows Explorer C:\Program Files\Java\jdk1.7.0_02\bin and ...READ MORE

answered Aug 6, 2019 in Java by Sirajul
• 59,230 points
51,495 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,140 points
3,740 views
0 votes
3 answers

How to check whether a file exists or not in Java?

Using nio we can check whether file ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,920 points
4,145 views
0 votes
2 answers

How to find out current working directory in Java?

You can also use java.nio.file.Path and java.nio.file.Paths. Path ...READ MORE

answered Jul 31, 2018 in Java by Sushmita
• 6,920 points
1,032 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP