AWS - Create an AmazonSNSClient

2020-07-31 05:47发布

问题:

I want to create a AmazonSNSClient, I use this piece of code:

AmazonSNSClient snsClient = (AmazonSNSClient) AmazonSNSClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(new PropertiesCredentials(is))).build();

but I get this error:

Exception in thread "main" java.lang.UnsupportedOperationException: Client is immutable when created with the builder.

at com.amazonaws.AmazonWebServiceClient.checkMutability(AmazonWebServiceClient.java:937)
at com.amazonaws.AmazonWebServiceClient.setRegion(AmazonWebServiceClient.java:422)

回答1:

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,

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();

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();