2017-06-05 61 views
0

使用AWSSecurityTokenServiceClientBuilder我用AWS - STS如何與全球區域

AWSSecurityTokenServiceClient sts_client = new AWSSecurityTokenServiceClient(), 

和默認的區域(全球)設置authomatically。但是這個構造函數被去除,推薦使用:

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) 

謝謝!

回答

0

我從來沒有聽說過全球地區,但指定與新的非棄用生成器方法的默認區域,您可以使用:

.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憑證可以用於所有地區(所以在某種意義上它們是'全球'的)。