AWS Polly Java Client gives error: Unable to load region information from any provider in the chain

AWS Polly Java Client gives error: Unable to load region information from any provider in the chain

我正在使用来自 AWS 的 JAVA SDK 创建 Polly 客户端。 像这样:

BasicAWSCredentials awsCreds = new BasicAWSCredentials("<IAM access Key>", "IAM secret key>");

    AmazonPollyClient apClient = (AmazonPollyClient) AmazonPollyClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
            .build();


    SynthesizeSpeechRequest tssRequest = new SynthesizeSpeechRequest();
    tssRequest.setText(<text>);
    tssRequest.setVoiceId(<voiceid>);
    tssRequest.setOutputFormat(OutputFormat.Mp3);
    SynthesizeSpeechResult tssResult = apClient.synthesizeSpeech(tssRequest);

当我 运行 此代码时,我收到以下错误消息:

Exception in thread "main" com.amazonaws.SdkClientException: Unable to load region information from any provider in the chain at com.amazonaws.regions.AwsRegionProviderChain.getRegion(AwsRegionProviderChain.java:56) at com.amazonaws.client.builder.AwsClientBuilder.setRegion(AwsClientBuilder.java:319) at com.amazonaws.client.builder.AwsClientBuilder.configureMutableProperties(AwsClientBuilder.java:295) at com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:38) at com.eoffice.aws.speech.Polly.main(Polly.java:42)

我使用 IAM 策略模拟器检查了凭据。这很好用,权限没问题。

在 ClientBuilder 中设置区域的方法对于 AmazonPollyClientBuilder 不可见,所以我没有(Java SDK)指定区域的方法。

更新: 当我询问 defaultAwsREgionProviderChain 时,我收到相同的错误消息

DefaultAwsRegionProviderChain defaultAwsRegionProviderChain = new DefaultAwsRegionProviderChain();
System.out.println(defaultAwsRegionProviderChain.getRegion());

更新 2: 当我在 de .aws 文件夹中创建一个包含以下内容的配置文件时:

[default] region = eu-west-1

它有效,但我需要一种不依赖文件系统的方法来设置它。

提供名称为 "AWS_REGION" 的系统环境变量就成功了。 在 IBM Bluemix

中查看配置的屏幕截图

我觉得你可以这样设置Region

AmazonPollyClient apClient = (AmazonPollyClient) AmazonPollyClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds)).withRegion("<aws-region>").build();