3

I have written a AWS Lambda Function, Its objective is that on invocation - it read the contents of a file say x.db, get a specific value out of it and return to the caller.But this x.db file changes time to time. So I would like to upload this x.db file to S3 and read it from AWS Lambda function as like reading a file.

        File xFile = new File("S3 file in x.db");

How to read such x.db S3 file from AWS Lambda Function written in Java ?

2
  • The same way you would read an S3 file from Java running anywhere. Commented Apr 6, 2016 at 22:20
  • Thanks Mark, I have never done that, freshly new to this area, I will try as suggested. I also searched before posting here, but didn't find anything. Commented Apr 6, 2016 at 22:22

1 Answer 1

11

Use the Java S3 SDK. If you upload a file called x.db to an S3 bucket mybucket, it would look something like this:

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

...
AmazonS3 client = new AmazonS3Client();
S3Object xFile = client.getObject("mybucket", "x.db");
InputStream contents = xFile.getObjectContent();

In addition, you should ensure the role you've assigned to your lambda function has access to the S3 bucket. Apply a policy like this:

"Version": "2012-10-17",
"Statement": [{
  "Effect": "Allow",
  "Action": [
    "s3:*"
  ],
  "Resource": [
    "arn:aws:s3:::mybucket",
    "arn:aws:s3:::mybucket/*"
  ]
}]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Atalylor,I will follow as suggested and then update here the result.
Thanks ataylor, this solution worked as suggested.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.