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 ?

Sumit Arora
  • 5,051
  • 7
  • 37
  • 57
  • The same way you would read an S3 file from Java running anywhere. – Mark B Apr 06 '16 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. – Sumit Arora Apr 06 '16 at 22:22

1 Answers1

10

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/*"
  ]
}]
ataylor
  • 64,891
  • 24
  • 161
  • 189