AWS-创建一个AmazonSNSClient [英] AWS - Create an AmazonSNSClient
问题描述
我想创建一个AmazonSNSClient,我使用这段代码:
I want to create a AmazonSNSClient, I use this piece of code:
AmazonSNSClient snsClient = (AmazonSNSClient) AmazonSNSClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(new PropertiesCredentials(is))).build();
但我收到此错误:
线程"main"中的异常java.lang.UnsupportedOperationException:使用构建器创建时客户端是不可变的.
Exception in thread "main" java.lang.UnsupportedOperationException: Client is immutable when created with the builder.
在com.amazonaws.AmazonWebServiceClient.checkMutability(AmazonWebServiceClient.java:937)
在com.amazonaws.AmazonWebServiceClient.setRegion(AmazonWebServiceClient.java:422)
at com.amazonaws.AmazonWebServiceClient.checkMutability(AmazonWebServiceClient.java:937)
at com.amazonaws.AmazonWebServiceClient.setRegion(AmazonWebServiceClient.java:422)
推荐答案
如果可以将已传递的参数放在is
中,否则可以尝试按以下方式构建客户端,这是更好的选择
It is better if you can put the parameters which you have passed as is
or else you can try building the client as below,
如果您的is
引用的是凭据文件,则可以直接通过此方法使用凭据,
If your is
is referring to a credential file then you can use the credentials directly with this method,
BasicAWSCredentials basicAwsCredentials = new BasicAWSCredentials(AccessKey,SecretAccessKey);
AmazonSNS snsClient = AmazonSNSClient
.builder()
.withRegion(your_region)
.withCredentials(new AWSStaticCredentialsProvider(basicAwsCredentials))
.build();
否则,如果您打算通过IAM角色授予权限,则可以使用InstanceProfileCredentialProvider,如下所示,
or else if you are going to give permission through an IAM role then you can use InstanceProfileCredentialProvider like below,
AmazonSNS sns = AmazonSNSClientBuilder
.standard()
.withCredentials(new InstanceProfileCredentialsProvider(true))
.build();
这篇关于AWS-创建一个AmazonSNSClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!