本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 AWS 区域 区和可用区
描述区域
要列出账户可用的区域,请调用 Ec2Client 的 describeRegions
方法。它返回 DescribeRegionsResponseregions
方法,获取表示各个区域的 Region
导入
import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.DescribeRegionsResponse; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse; import software.amazon.awssdk.services.ec2.model.Ec2Exception;
代码
try { DescribeRegionsResponse regionsResponse = ec2.describeRegions(); regionsResponse.regions().forEach(region -> { System.out.printf( "Found Region %s with endpoint %s%n", region.regionName(), region.endpoint()); System.out.println(); });
请参阅上的完整示例
描述可用区
要列出账户可用的每个可用区,请调用 Ec2Client 的 describeAvailabilityZones
方法。它返回 DescribeAvailabilityZonesResponseavailabilityZones
方法以获取代表每个可用区的AvailabilityZone
导入
import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.DescribeRegionsResponse; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse; import software.amazon.awssdk.services.ec2.model.Ec2Exception;
代码
创建 Ec2Client。
software.amazon.awssdk.regions.Region region = software.amazon.awssdk.regions.Region.US_EAST_1; Ec2Client ec2 = Ec2Client.builder() .region(region) .build();
然后调用 describeAvailabilityZones () 并检索结果。
DescribeAvailabilityZonesResponse zonesResponse = ec2.describeAvailabilityZones(); zonesResponse.availabilityZones().forEach(zone -> { System.out.printf( "Found Availability Zone %s with status %s in region %s%n", zone.zoneName(), zone.state(), zone.regionName() ); System.out.println(); });
请参阅上的完整示例
描述账户
要列出有关账户的 EC2 相关信息,请调用 Ec2Client 的 describeAccountAttributes
方法。此方法返回一个DescribeAccountAttributesResponseaccountAttributes
方法以获取AccountAttributeAccountAttribute
对象。
您可以通过调用AccountAttribute
对象的attributeValues
方法来获取账户的属性值。此方法返回AccountAttributeValue
导入
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.DescribeAccountAttributesResponse; import software.amazon.awssdk.services.ec2.model.Ec2Exception;
代码
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.DescribeAccountAttributesResponse; import software.amazon.awssdk.services.ec2.model.Ec2Exception; /** * 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) { Region region = Region.US_EAST_1; Ec2Client ec2 = Ec2Client.builder() .region(region) .build(); describeEC2Account(ec2); System.out.print("Done"); ec2.close(); } public static void describeEC2Account(Ec2Client ec2) { try { DescribeAccountAttributesResponse accountResults = ec2.describeAccountAttributes(); accountResults.accountAttributes().forEach(attribute -> { System.out.print("\n The name of the attribute is " + attribute.attributeName()); attribute.attributeValues().forEach( myValue -> System.out.print("\n The value of the attribute is " + myValue.attributeValue())); }); } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
请参阅上的完整示例
更多信息
-
Linux 实例 Amazon EC2 用户指南中的@@ 区域和可用区
-
DescribeRegions在 Amazon EC2 API 参考中
-
DescribeAvailabilityZones在 Amazon EC2 API 参考中