如何从用Java编写的AWS Lambda Function中读取S3文件? [英] How to read S3 file from AWS Lambda Function written in Java?

查看:332
本文介绍了如何从用Java编写的AWS Lambda Function中读取S3文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个AWS Lambda函数,其目的是在调用时-它读取文件x.db的内容,从中获取特定值并返回给调用者.但是这个x.db文件发生了变化时不时.因此,我想将此x.db文件上传到S3并从AWS Lambda函数读取它,就像读取文件一样.

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");

如何从用Java编写的AWS Lambda Function中读取此类x.db S3文件?

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

推荐答案

使用Java S3 SDK.如果您将名为x.db的文件上载到S3存储桶mybucket,它将看起来像这样:

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();

此外,您应该确保分配给lambda函数的角色可以访问S3存储桶.应用这样的策略:

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/*"
  ]
}]

这篇关于如何从用Java编写的AWS Lambda Function中读取S3文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆