AWS Lambda:如何使用java从Lambda函数访问S3存储桶 [英] AWS Lambda : How to access S3 bucket from Lambda function using java

查看:1294
本文介绍了AWS Lambda:如何使用java从Lambda函数访问S3存储桶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个Lambda函数。
此函数上传到s3Bucket =my-lambda,映射到角色hello-lambda-role,regionName =us-west-2。

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".

现在我想访问s3Bucket =some-other,我们用'hello-lambda-role'映射了Policy,它位于eu-west-1区域。

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".

这是我正在使用的API类 AmazonS3Client 。我的目的是从桶中获取一些文件some-other。但在此之前,我需要建立连接。

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.

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

}

这是列出的类桶。

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;
    }
}


推荐答案

使用与您在测试中使用的代码相同,但在创建 AmazonS3Client 时不需要提供凭据。请注意,lambda使用的角色需要权限才能访问S3存储桶。 S3桶的区域无关紧要;无论区域如何,存储桶名称都唯一标识存储桶。

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.

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

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

例如:

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

将其作为mylambda部署到AWS,然后远程调用:

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

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

这篇关于AWS Lambda:如何使用java从Lambda函数访问S3存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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