当我收到一条错误消息时,我正在编写代码以在Eclipse中创建Amazon Web Services SNS客户端。


  类型中的withRegion(Region)方法
   AwsClientBuilder不可见


这是我的代码

package com.amazonaws.samples;

import java.util.Date;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.AnonymousAWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.AmazonSNSClientBuilder;
import com.amazonaws.services.sns.model.CreateTopicRequest;
import com.amazonaws.services.sns.model.CreateTopicResult;
import com.amazonaws.services.sns.model.PublishRequest;

// Example SNS Sender
public class Main {

    // AWS credentials -- replace with your credentials
    static String ACCESS_KEY = "<Your AWS Access Key>";
    static String SECRET_KEY = "<Your AWS Secret Key>";

    // Sender loop
    public static void main(String... args) throws Exception {

        // Create a client

        AWSCredentials awsCred = new AnonymousAWSCredentials();

        AWSStaticCredentialsProvider cred = new AWSStaticCredentialsProvider(awsCred);

        Region region = Region.getRegion(Regions.US_EAST_1);

        AmazonSNS service = AmazonSNSClientBuilder.standard().withRegion(region).withCredentials(cred).build(); // Error message: The method withRegion(Region) from the type AwsClientBuilder<AmazonSNSClientBuilder,AmazonSNS> is not visible

        // Create a topic
        CreateTopicRequest createReq = new CreateTopicRequest()
            .withName("MyTopic3");
        CreateTopicResult createRes = service.createTopic(createReq);

        for (;;) {

            // Publish to a topic
            PublishRequest publishReq = new PublishRequest()
                .withTopicArn(createRes.getTopicArn())
                .withMessage("Example notification sent at " + new Date());
            service.publish(publishReq);

            Thread.sleep(1000);
        }
    }
}


在屏幕截图中,它用虚线的红色下划线显示了错误发生的位置:

java - 为什么看不到AmazonSNSClientBuilder的withRegion()?-LMLPHP

我应该检查些什么来纠正这个问题?

最佳答案

您传递了错误的参数,withRegion接受了StringRegions(请注意,不是Region,单数)。

尝试传递Regions.EU_WEST_1

两者AmazonSNSClientBuilder.standard().withRegion(Regions.EU_WEST_1).build();

AmazonSNSClientBuilder.standard().withRegion("eu-west-1").build();

对我来说很好。

08-04 04:29