AWS Java SDK - 无法通过区域提供商链找到区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44151982/
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-08-12 01:23:09  来源:igfitidea点击:

AWS Java SDK - Unable to find a region via the region provider chain

javaamazon-web-servicesamazon-s3aws-lambdaaws-sdk

提问by Codistan

I have gone through the question titled "Setting the AWS region programmatically 1" but it doesn't provide all the answers I need.

我已经完成了题为“以编程方式设置 AWS 区域 1”的问题,但它没有提供我需要的所有答案。

Q1: I'm getting a SDKClientException-Unable to find a region via the region provider chain. What am I doing wrong? or is there a typo that I missed.

Q1:我得到了一个SDKClientException-Unable to find a region via the region provider chain. 我究竟做错了什么?或者我错过了一个错字。

public class CreateS3Bucket {

public static void main(String[] args) throws IOException {

    BasicAWSCredentials creds = new BasicAWSCredentials("aws-access-key", "aws-secret-key");
    AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).build();

    Region region = Region.getRegion(Regions.US_EAST_1);
    s3Client.setRegion(region);

    try {
        String bucketName = "testBucket" + UUID.randomUUID();
        s3Client.createBucket(bucketName);
        System.out.println("Bucket Created Successfully.");

    } catch(AmazonServiceException awse) {

        System.out.println("This means that your request made it AWS S3 but got rejected");
        System.out.println("Error Message:" +awse.getMessage());
        System.out.println("Error Message:" +awse.getErrorCode());
        System.out.println("Error Message:" +awse.getErrorType());
        System.out.println("Error Message:" +awse.getRequestId());

    } catch (AmazonClientException ace) {

        System.out.println("The Amazon Client encountered an Error with network Connectivity");
        System.out.println("Error Message:" + ace.getMessage());
    }


}

}

}

Q2: What code changes needs to be done if I want to build a Lambda Function out of it? I'm aware how to create a lambda function and roles that it needs. Just need to know if the code that I have written needs to changed. How should I implement the LambdaFuctionHandler class as below:

Q2:如果我想从中构建一个 Lambda 函数,需要做哪些代码更改?我知道如何创建一个 lambda 函数和它需要的角色。只需要知道我写的代码是否需要更改。我应该如何实现 LambdaFuctionHandler 类,如下所示:

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

 public class LambdaFunctionHandler implements RequestHandler<String, String> {

@Override
public String handleRequest(String input, Context context) {
    context.getLogger().log("Input: " + input);


    return null;
}

}

采纳答案by mapm

Regarding Q1, try to build your client using the following syntax:

关于 Q1,尝试使用以下语法构建您的客户端:

AmazonS3 amazonS3 = AmazonS3Client.builder()
    .withRegion("us-east-1")
    .withCredentials(new AWSStaticCredentialsProvider(creds))
    .build();

回答by ian1095

Well steps you can take to investigate:

您可以采取以下步骤进行调查:

Please make sure your Lambda function and S3 are in the same region. (When you use ProviderChain, it will pick up the region from the Lambda function

请确保您的 Lambda 函数和 S3 在同一区域。(当您使用 ProviderChain 时,它将从 Lambda 函数中选取区域

Also, You should not need to specify the BasicCredentials(aws-key..etc) if you are using Lambda.

此外,如果您使用的是 Lambda,则不需要指定 BasicCredentials(aws-key..etc)。

Please read about Lambda Permission model (http://docs.aws.amazon.com/lambda/latest/dg/intro-permission-model.html):

请阅读 Lambda 权限模型(http://docs.aws.amazon.com/lambda/latest/dg/intro-permission-model.html):

Basically, the Lambda role that you assign should have permission to access S3.

基本上,您分配的 Lambda 角色应该有权访问 S3。

All you need to configure S3 is pretty much:

您只需要配置 S3:

private static final AmazonS3 s3Client = 
AmazonS3ClientBuilder.defaultClient();

To test it locally, make sure you have configured the AWS Credentials locally.

要在本地进行测试,请确保您已在本地配置 AWS 凭证。

You can check if you have the credentials, if you go into .aws/credentials (This will contain the "aws-access-key", "aws-secret-key")

如果您进入 .aws/credentials(这将包含“aws-access-key”、“aws-secret-key”),您可以检查您是否拥有凭据

http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html

http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html

To set up your credentials locally, all you need to do is run the AWS Cli command: aws configure (http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.htm)

要在本地设置凭证,您需要做的就是运行 AWS Cli 命令:aws configure ( http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.htm)

回答by Aniket Thakur

As mentioned in the answer above you need to have S3 and lambda in same region and here's why-

正如上面的答案中提到的,您需要在同一区域拥有 S3 和 lambda,这就是原因-

If you don't explicitly set a region using the withRegionmethods, the SDK consults the default region provider chain to try and determine the region to use. One of the methods used is -

如果您未使用这些withRegion方法显式设置区域,则 SDK 会咨询默认区域提供程序链以尝试确定要使用的区域。使用的方法之一是 -

The AWS_REGION environment variable is checked. If it's set, that region is used to configure the client.

检查 AWS_REGION 环境变量。如果已设置,则该区域用于配置客户端。

And in the case of Lambda -

而在 Lambda 的情况下 -

This environment variable is set by the Lambda container.

此环境变量由 Lambda 容器设置。

Finally, to use default credential/region provider chain to determine the region from the environment, use the client builder's defaultClient method.

最后,要使用默认凭据/区域提供程序链从环境中确定区域,请使用客户端构建器的 defaultClient 方法。

AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();

This is the same as using standard followed by build.

这与使用标准后跟构建相同。

AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();

AWS Documentation: https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/java-dg-region-selection.html

AWS 文档:https: //docs.aws.amazon.com/sdk-for-java/v1/developer-guide/java-dg-region-selection.html

More details: How to fix "Unable to find a region via the region provider chain" exception with AWS SDK

更多详细信息:如何使用 AWS SDK 修复“无法通过区域提供商链找到区域”异常

PS: Above link goes to my personal blog that has additional details on this.

PS:以上链接转到我的个人博客,其中包含有关此的其他详细信息。

回答by Akhilesh Nagabhushan

This worked for me.

这对我有用。

AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(Regions.AP_SOUTH_1).build();

回答by Peter.Chu

You can try this as below

你可以试试这个

BasicAWSCredentials creds = new BasicAWSCredentials("your-accessKey", "your-secretKey");
AmazonSNS snsClient = AmazonSNSClientBuilder.standard()
        .withCredentials(new AWSStaticCredentialsProvider(creds))
        .withRegion(Regions.US_EAST_1).build();

回答by Kumar Abhishek

Actually, while I faced this issue was not possible to update the code all the time.

实际上,虽然我遇到了这个问题,但不可能一直更新代码。

Thus we need to look into the reasons why it's happening, While investigating I found there is .awsfolder created at your root. It was created because I tried to install aws consolesome time back.

因此,我们需要调查发生这种情况的原因,在调查时我发现.aws在您的根目录下创建了一个文件夹。它是因为我尝试安装aws console一段时间而创建的。

The solution is you need to update your config file with region.

解决方案是您需要使用区域更新您的配置文件。

回答by Ramshad

withRegion(Regions.DEFAULT_REGION)helped me to solve my issue

withRegion(Regions.DEFAULT_REGION)帮助我解决了我的问题

return AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.withRegion(Regions.DEFAULT_REGION)
.build();