AWS - STS如何将AWSSecurityTokenServiceClientBuilder与全局区域一起使用

时间:2017-06-05 08:02:08

标签: aws-sts

我用过

AWSSecurityTokenServiceClient sts_client = new AWSSecurityTokenServiceClient(), 

并自动设置默认区域(全局)。但是这个构造函数已被删除,建议使用:

AWSSecurityTokenServiceClientBuilder.

我希望它也使用默认区域。我写道:

AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build();

但我有一个例外:

com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region.

at com.amazonaws.client.builder.AwsClientBuilder.setRegion(AwsClientBuilder.java:371)
at com.amazonaws.client.builder.AwsClientBuilder.configureMutableProperties(AwsClientBuilder.java:337)
at com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46)
at co.softimize.STSManager.<init>(STSManager.java:31)
at co.softimize.sts.STSManagerTests.setup(STSManagerTests.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

谢谢!

1 个答案:

答案 0 :(得分:3)

我从来没有听说过Global区域,但是要使用新的非弃用Builder方法指定默认区域,您可以使用:

.withRegion(Regions.DEFAULT_REGION)

所以你的命令是:

AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) .withRegion(Regions.DEFAULT_REGION.getName()) .build();

仅供参考 - 在我的版本区域枚举中,DEFAULT_REGIONUS_WEST_2。别忘了

import com.amazonaws.regions.Regions;

编辑:知道您指定的区域并不重要可能会有用。生成的STS凭证可以在所有地区使用(因此它们在某种意义上是全球性的。)