简体   繁体   English

AWS Java SDK v2 应将哪个区域终端节点用于 Route 53?

[英]Which region endpoint should AWS Java SDK v2 be using for Route 53?

On Windows 10 I'm using the AWS Java SDK v2 ( software.amazon.awssdk:route53:2.8.3 ) and I'm trying to merely connect and list all my Route 53 hosted zones. On Windows 10 I'm using the AWS Java SDK v2 ( software.amazon.awssdk:route53:2.8.3 ) and I'm trying to merely connect and list all my Route 53 hosted zones. I have us-west-1 specified in my user configuration (in my .aws/config file) as the default region.我在我的用户配置(在我的.aws/config文件中)中指定了us-west-1作为默认区域。

I create a Route53Client using the following:我使用以下内容创建了一个Route53Client

Route53Client route53Client = Route53Client.builder().build();

Note that I don't indicate a region, because in the online documentation it says:请注意,我没有指明地区,因为在在线文档中它说:

When you submit requests using the AWS CLI or SDKs, either leave the Region and endpoint unspecified, or specify us-east-1 as the Region.当您使用 AWS CLI 或开发工具包提交请求时,请不要指定区域和终端节点,或者将 us-east-1 指定为区域。

I then try to list hosted zones using something like this:然后我尝试使用以下内容列出托管区域:

Set<HostedZone> hostedZones = client.listHostedZonesPaginator().stream()
  .flatMap(response -> response.hostedZones().stream())
  .collect(Collectors.toSet());

In the logs I see a debug message like this:在日志中,我看到如下调试消息:

[DEBUG] Unable to load region from software.amazon.awssdk.regions.providers.SystemSettingsRegionProvider@...:Unable to load region from system settings. Region must be specified either via environment variable (AWS_REGION) or system property (aws.region).

Then it throws a java.net.UnknownHostException for route53.us-west-1.amazonaws.com .然后它为route53.us-west-1.amazonaws.com引发java.net.UnknownHostException

Granted I am on a spotty Internet connection right now.诚然,我现在的 Internet 连接参差不齐。 Is that the correct endpoint?那是正确的端点吗? If it is, the why isn't that endpoint listed at https://docs.aws.amazon.com/general/latest/gr/rande.html ?如果是,为什么不在https://docs.aws.amazon.com/general/latest/gr/rande.html中列出该端点? If it isn't, why is it trying to connect to a us-west1 endpoint, if I'm following the online documentation (as I quoted above), which indicates that a region need not be indicated?如果不是,为什么它要尝试连接到us-west1端点,如果我遵循在线文档(如我上面引用的),这表明不需要指定区域? Or is the problem simply my Internet connection and spotty DNS lookup at the moment?或者问题仅仅是我的 Internet 连接和目前零星的 DNS 查找?

The AWS SDK development team decided to require Route53 requests to explicitly indicate the Region.AWS_GLOBAL or requests would not work, as someone noted in Issue #456 for the SDK: AWS SDK 开发团队决定要求 Route53 请求明确指出Region.AWS_GLOBAL或请求将不起作用,正如 SDK 的问题 #456中有人指出的那样:

To access Route53 you would currently need to specify the AWS_GLOBAL region.要访问 Route53,您当前需要指定 AWS_GLOBAL 区域。 This was done to prevent customers from using global services and not realizing that for this service your calls are likely not staying in region and could potentially be spanning the globe.这样做是为了防止客户使用全球服务,而没有意识到对于这项服务,您的呼叫可能不会停留在区域内,并且可能跨越全球。

Unfortunately Amazon didn't bother documenting this in the SDK (that I could find), and didn't provide a helpful error message, instead assuming developers would somehow guess the problem when the SDK tried to access an endpoint that did not exist even though the SDK was being used according to the API and according to the online documentation.不幸的是,亚马逊没有费心在 SDK(我可以找到)中记录这一点,也没有提供有用的错误消息,而是假设开发人员会在 SDK 尝试访问不存在的端点时以某种方式猜测问题,即使根据 API 和在线文档,正在使用 SDK。

In short the Route53 client must be created like this:简而言之,必须像这样创建 Route53 客户端:

route53Client = Route53Client.builder().region(Region.AWS_GLOBAL).build();

Here is the AWS Route 53 V2 Code example that lists hosted zones:以下是列出托管区域的 AWS Route 53 V2 代码示例:

package com.example.route;

//snippet-start:[route.java2.list_zones.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.route53.Route53Client;
import software.amazon.awssdk.services.route53.model.HostedZone;
import software.amazon.awssdk.services.route53.model.Route53Exception;
import software.amazon.awssdk.services.route53.model.ListHostedZonesResponse;
import java.util.List;
//snippet-end:[route.java2.list_zones.import]

public class ListHostedZones {
    public static void main(String[] args) {

        Region region = Region.AWS_GLOBAL;
        Route53Client route53Client = Route53Client.builder()
                .region(region)
                .build();

        listZones(route53Client);
    }

    //snippet-start:[route.java2.list_zones.main]
    public static void listZones(Route53Client route53Client) {

        try {

            ListHostedZonesResponse zonesResponse = route53Client.listHostedZones();
            List<HostedZone> checklist = zonesResponse.hostedZones();

            for (HostedZone check: checklist) {
                System.out.println("The name is : "+check.name());
            }

        } catch (Route53Exception e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
    }
    //snippet-end:[route.java2.list_zones.main]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM