codeexplorer
codeexplorer

Reputation: 459

Java AWSClient reference in lambda

I am using a lambda function written in java that listen to s3 bucket create object The requesthandler will call another class "testDownload" to download the object

something is happening when AmazonS3 s3 = AmazonS3ClientBuilder.standard().build(); is initialized, i am using it in a try catch block and checking if an exception is thrown and no error is displaying in cloud watch. Can you please help me understand whats going on

public class S3EventProcessor implements RequestHandler<S3Event, String> {
private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());
private final String ASSET_TYPE = (String) "jpg";
public String handleRequest(S3Event s3event, Context context) {
....
....
t.download(Bucket,Key);


import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class testDownload {
private final org.slf4j.Logger logger =  LoggerFactory.getLogger(this.getClass());

public void download(String bucketName,String key) throws IOException {


    try {
        logger.info("Downloading an object \n");
        logger.info("Building s3 client");
        AmazonS3 s3 = AmazonS3ClientBuilder.standard().build();
        logger.info("success");
        S3Object o = s3.getObject(bucketName, key);
        S3ObjectInputStream s3is = o.getObjectContent();
        FileOutputStream fos = new FileOutputStream(new File(key));
        byte[] read_buf = new byte[1024];
        int read_len = 0;
        while ((read_len = s3is.read(read_buf)) > 0) {
            fos.write(read_buf, 0, read_len);
        }
        s3is.close();
        fos.close();
    } catch (AmazonServiceException e) {
        logger.error(e.getErrorMessage());
        System.exit(1);
    } catch (FileNotFoundException e) {
        logger.error(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        logger.error(e.getMessage());
        System.exit(1);
    } catch (Exception e){
        logger.error(e.getStackTrace().toString());
        System.exit(1);
    }
}

}

Cloud Logs

19:25:34,229 INFO testDownload:24 - Downloading an object 

19:25:34,229 INFO testDownload:25 - Building s3 client
END RequestId: someid
REPORT RequestId: someid    Duration:   3003.13 ms  Billed Duration: 3000 ms Memory Size: 128 MB    Max Memory     Used: 52 MB  
2017-10-21T19:25:36.150Z someid Task timed out after 3.00 seconds

Upvotes: 0

Views: 113

Answers (1)

stdunbar
stdunbar

Reputation: 17535

The default timeout for a Lambda is 3 seconds. Your Lambda runs for 3 seconds and then is killed. Try to configure the Lambda to run longer. Additionally, what are you trying to do with the output file - save it on the Lambda machine?

Upvotes: 1

Related Questions