使用 Amazon Web Services 区域 区和可用区 - Amazon SDK for Java 2.x
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon Web Services 区域 区和可用区

描述区域

要列出账户可用的区域,请调用 Ec2Client 的 describeRegions 方法。它返回 DescribeRegionsResponse。调用返回对象的 regions 方法,获取表示各个区域的 Region 对象的列表。

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2AsyncClient; import software.amazon.awssdk.services.ec2.model.DescribeRegionsResponse; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse; import java.util.concurrent.CompletableFuture;

代码

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2AsyncClient; import software.amazon.awssdk.services.ec2.model.DescribeRegionsResponse; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse; import java.util.concurrent.CompletableFuture; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DescribeRegionsAndZones { public static void main(String[] args) { Ec2AsyncClient ec2AsyncClient = Ec2AsyncClient.builder() .region(Region.US_EAST_1) .build(); try { CompletableFuture<Void> future = describeEC2RegionsAndZonesAsync(ec2AsyncClient); future.join(); // Wait for both async operations to complete. } catch (RuntimeException rte) { System.err.println("An exception occurred: " + (rte.getCause() != null ? rte.getCause().getMessage() : rte.getMessage())); } } /** * Asynchronously describes the EC2 regions and availability zones. * * @param ec2AsyncClient the EC2 async client used to make the API calls * @return a {@link CompletableFuture} that completes when both the region and availability zone descriptions are complete */ public static CompletableFuture<Void> describeEC2RegionsAndZonesAsync(Ec2AsyncClient ec2AsyncClient) { // Initiate the asynchronous request to describe regions CompletableFuture<DescribeRegionsResponse> regionsResponse = ec2AsyncClient.describeRegions(); // Handle the response or exception for regions CompletableFuture<DescribeRegionsResponse> regionsFuture = regionsResponse.whenComplete((regionsResp, ex) -> { if (ex != null) { // Handle the exception by throwing a RuntimeException throw new RuntimeException("Failed to describe EC2 regions.", ex); } else if (regionsResp == null || regionsResp.regions().isEmpty()) { // Throw an exception if the response is null or the result is empty throw new RuntimeException("No EC2 regions found."); } else { // Process the response if no exception occurred and the result is not empty regionsResp.regions().forEach(region -> { System.out.printf( "Found Region %s with endpoint %s%n", region.regionName(), region.endpoint()); }); } }); CompletableFuture<DescribeAvailabilityZonesResponse> zonesResponse = ec2AsyncClient.describeAvailabilityZones(); CompletableFuture<DescribeAvailabilityZonesResponse> zonesFuture = zonesResponse.whenComplete((zonesResp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to describe EC2 availability zones.", ex); } else if (zonesResp == null || zonesResp.availabilityZones().isEmpty()) { throw new RuntimeException("No EC2 availability zones found."); } else { zonesResp.availabilityZones().forEach(zone -> { System.out.printf( "Found Availability Zone %s with status %s in region %s%n", zone.zoneName(), zone.state(), zone.regionName() ); }); } }); return CompletableFuture.allOf(regionsFuture, zonesFuture); } }

请参阅上的完整示例 GitHub。

描述可用区

要列出账户可用的每个可用区,请调用 Ec2Client 的 describeAvailabilityZones 方法。它返回 DescribeAvailabilityZonesResponse。调用其availabilityZones方法以获取代表每个可用区的AvailabilityZone对象列表。

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2AsyncClient; import software.amazon.awssdk.services.ec2.model.DescribeRegionsResponse; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse; import java.util.concurrent.CompletableFuture;

代码

创建 Ec2Client。

Ec2AsyncClient ec2AsyncClient = Ec2AsyncClient.builder() .region(Region.US_EAST_1) .build();

然后调用 describeAvailabilityZones () 并检索结果。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2AsyncClient; import software.amazon.awssdk.services.ec2.model.DescribeRegionsResponse; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse; import java.util.concurrent.CompletableFuture; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DescribeRegionsAndZones { public static void main(String[] args) { Ec2AsyncClient ec2AsyncClient = Ec2AsyncClient.builder() .region(Region.US_EAST_1) .build(); try { CompletableFuture<Void> future = describeEC2RegionsAndZonesAsync(ec2AsyncClient); future.join(); // Wait for both async operations to complete. } catch (RuntimeException rte) { System.err.println("An exception occurred: " + (rte.getCause() != null ? rte.getCause().getMessage() : rte.getMessage())); } } /** * Asynchronously describes the EC2 regions and availability zones. * * @param ec2AsyncClient the EC2 async client used to make the API calls * @return a {@link CompletableFuture} that completes when both the region and availability zone descriptions are complete */ public static CompletableFuture<Void> describeEC2RegionsAndZonesAsync(Ec2AsyncClient ec2AsyncClient) { // Initiate the asynchronous request to describe regions CompletableFuture<DescribeRegionsResponse> regionsResponse = ec2AsyncClient.describeRegions(); // Handle the response or exception for regions CompletableFuture<DescribeRegionsResponse> regionsFuture = regionsResponse.whenComplete((regionsResp, ex) -> { if (ex != null) { // Handle the exception by throwing a RuntimeException throw new RuntimeException("Failed to describe EC2 regions.", ex); } else if (regionsResp == null || regionsResp.regions().isEmpty()) { // Throw an exception if the response is null or the result is empty throw new RuntimeException("No EC2 regions found."); } else { // Process the response if no exception occurred and the result is not empty regionsResp.regions().forEach(region -> { System.out.printf( "Found Region %s with endpoint %s%n", region.regionName(), region.endpoint()); }); } }); CompletableFuture<DescribeAvailabilityZonesResponse> zonesResponse = ec2AsyncClient.describeAvailabilityZones(); CompletableFuture<DescribeAvailabilityZonesResponse> zonesFuture = zonesResponse.whenComplete((zonesResp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to describe EC2 availability zones.", ex); } else if (zonesResp == null || zonesResp.availabilityZones().isEmpty()) { throw new RuntimeException("No EC2 availability zones found."); } else { zonesResp.availabilityZones().forEach(zone -> { System.out.printf( "Found Availability Zone %s with status %s in region %s%n", zone.zoneName(), zone.state(), zone.regionName() ); }); } }); return CompletableFuture.allOf(regionsFuture, zonesFuture); } }

请参阅上的完整示例 GitHub。

描述账户

要列出有关您账户的EC2相关信息,请调用 Ec2Client describeAccountAttributes 的方法。此方法返回一个DescribeAccountAttributesResponse对象。调用此对象accountAttributes方法以获取AccountAttribute对象列表。您可以遍历列表以检索AccountAttribute对象。

您可以通过调用AccountAttribute对象的attributeValues方法来获取账户的属性值。此方法返回AccountAttributeValue对象列表。您可以遍历第二个列表来显示属性的值(请参阅以下代码示例)。

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2AsyncClient; import software.amazon.awssdk.services.ec2.model.DescribeAccountAttributesResponse; import java.util.concurrent.CompletableFuture;

代码

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2AsyncClient; import software.amazon.awssdk.services.ec2.model.DescribeAccountAttributesResponse; import java.util.concurrent.CompletableFuture; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DescribeAccount { public static void main(String[] args) { Ec2AsyncClient ec2AsyncClient = Ec2AsyncClient.builder() .region(Region.US_EAST_1) .build(); try { CompletableFuture<DescribeAccountAttributesResponse> future = describeEC2AccountAsync(ec2AsyncClient); future.join(); System.out.println("EC2 Account attributes described successfully."); } catch (RuntimeException rte) { System.err.println("An exception occurred: " + (rte.getCause() != null ? rte.getCause().getMessage() : rte.getMessage())); } } /** * Describes the EC2 account attributes asynchronously. * * @param ec2AsyncClient the EC2 asynchronous client to use for the operation * @return a {@link CompletableFuture} containing the {@link DescribeAccountAttributesResponse} with the account attributes */ public static CompletableFuture<DescribeAccountAttributesResponse> describeEC2AccountAsync(Ec2AsyncClient ec2AsyncClient) { CompletableFuture<DescribeAccountAttributesResponse> response = ec2AsyncClient.describeAccountAttributes(); return response.whenComplete((accountResults, ex) -> { if (ex != null) { // Handle the exception by throwing a RuntimeException. throw new RuntimeException("Failed to describe EC2 account attributes.", ex); } else if (accountResults == null || accountResults.accountAttributes().isEmpty()) { // Throw an exception if the response is null or no account attributes are found. throw new RuntimeException("No account attributes found."); } else { // Process the response if no exception occurred. accountResults.accountAttributes().forEach(attribute -> { System.out.println("\nThe name of the attribute is " + attribute.attributeName()); attribute.attributeValues().forEach( myValue -> System.out.println("The value of the attribute is " + myValue.attributeValue())); }); } }); } }

请参阅上的完整示例 GitHub。

更多信息