gpt4 book ai didi

java - 为什么 AmazonSNSClientBuilder 的 withRegion() 不可见?

转载 作者:行者123 更新时间:2023-12-02 09:51:40 24 4
gpt4 key购买 nike

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

The method withRegion(Region) from the type AwsClientBuilder is not visible

这是我的代码

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

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

Eclipse for Java withRegion() underlined in red to show error

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

最佳答案

您传递了错误的参数,withRegion 采用 StringRegions(注意,不是 Region >,单数)。

尝试传递Regions.EU_WEST_1

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

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

对我来说工作得很好。

关于java - 为什么 AmazonSNSClientBuilder 的 withRegion() 不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56277899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com