AWS Lambda:如何使用 java 从 Lambda 函数访问 S3 存储桶

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34760553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:17:44  来源:igfitidea点击:

AWS Lambda : How to access S3 bucket from Lambda function using java

javaamazon-web-servicesamazon-s3aws-lambda

提问by

I have written a Lambda function. This function is uploaded in s3Bucket = "my-lambda", which is mapped to the role hello-lambda-role and the regionName = "us-west-2".

我写了一个 Lambda 函数。这个函数是在 s3Bucket = "my-lambda" 中上传的,它映射到角色 hello-lambda-role 和 regionName = "us-west-2"。

Now I wanted to access s3Bucket="some-other" where we have mapped Policy with 'hello-lambda-role' and it is in the region "eu-west-1".

现在我想访问 s3Bucket="some-other",其中我们已将 Policy 映射到“hello-lambda-role”,并且它位于“eu-west-1”区域中。

Here is the API class I am using AmazonS3Client. My intention is to get some file from the bucket "some-other". But before that I need to make the connection.

这是我使用的 API 类AmazonS3Client。我的目的是从存储桶“其他”中获取一些文件。但在此之前,我需要建立联系。

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaFunctionHandler implements RequestHandler<Request, Response> {

    public Response handleRequest(Request request, Context context) {
        String greetingString = String.format("Hello %s %s.",
                request.firstName, request.lastName);
        return new Response(greetingString);
    }

}

Here is the class which list the bucket.

这是列出存储桶的类。

public class Test{
    private static final Log logger = LogFactory.getLog(InvokeLambda.class);
    private static final String awsAccessKeyId = "XXXXX";
    private static final String awsSecretAccessKey = "XXXXX";
    private static final String regionName = "eu-west-1";
    private static Region region;
    private static AWSCredentials credentials;
    private static AWSLambdaClient lambdaClient;
    private static AmazonS3Client s3Client;

    private static void init() {
        credentials = new BasicAWSCredentials(awsAccessKeyId,
                awsSecretAccessKey);
        s3Client = (credentials == null) ? new AmazonS3Client()
                : new AmazonS3Client(credentials);
        region = Region.getRegion(Regions.fromName(regionName));
        s3Client.setRegion(region);
        lambdaClient = (credentials == null) ? new AWSLambdaClient()
                : new AWSLambdaClient(credentials);

        lambdaClient.setRegion(region);
        // lambdaClient.configureRegion(Regions.US_WEST_2);
    }

    /**
     * The entry point into the AWS lambda function.
     */
    public static void main(String... args) {
        init();
        getExistingBucket();
    }

    private static Bucket getExistingBucket() {
        List<Bucket> buckets = s3Client.listBuckets();
        for (Bucket bucket : buckets) {
            logger.error(bucket.getName());
        }
        return null;
    }
}

采纳答案by ataylor

Use same code as you would in your test, except you don't need to provide credentials when you create the AmazonS3Client. Note that the role your lambda uses needs the authority to access your S3 bucket. The region of the S3 bucket shouldn't matter; the bucket name uniquely identifies the bucket regardless of region.

使用与测试中相同的代码,但在创建AmazonS3Client. 请注意,您的 lambda 使用的角色需要有权访问您的 S3 存储桶。S3 存储桶的区域应该无关紧要;桶名唯一标识桶,不分地域。

Lambdas are typically triggered by an event, but you can call AWSLambdaClient.invoke()to run it manually.

Lambda 通常由事件触发,但您可以调用AWSLambdaClient.invoke()以手动运行它。

For example:

例如:

public Response handleRequest(Request request, Context context) {
    AmazonS3Client s3Client = new AmazonS3Client();
    S3Object = s3Client.getObject("some-other", request.getFilename());
    ....
    return new Response(result);
}

Deploy that to AWS as "mylambda", and then invoke remotely with:

将其作为“mylambda”部署到 AWS,然后使用以下命令远程调用:

lambdaClient.invoke(new InvokeRequest().withFunctionName("mylambda").withPayload("input"));