gpt4 book ai didi

amazon-web-services - 现在有办法在 boto3 中获取 "AWS region names"吗?

转载 作者:行者123 更新时间:2023-12-04 18:18:48 27 4
gpt4 key购买 nike

现在 boto3 是否有办法将 AWS 区域代码转换为 AWS 区域名称,例如将 ('us-west-1', 'us-east-1', 'us-west-2') 转换为 ('N. California', 'N. Virginia', 'Oregon')

我可以使用以下代码段获取 AWS 区域代码列表:

 from boto3.session import Session
s = Session()
regions = s.get_available_regions('rds')
print("regions:", regions)

$ python3 regions.py
regions: ['ap-northeast-1', 'ap-northeast-2', 'ap-south-1', 'ap-southeast-1', 'ap-southeast-2', 'ca-central-1', 'eu-central-1', 'eu-west-1', 'eu-west-2', 'eu-west-3', 'sa-east-1', 'us-east-1', 'us-east-2', 'us-west-1', 'us-west-2']

是否有一个等效的片段可以给我 AWS 区域名称?

最佳答案

这是我得到的解决方案,它显示了所有 AWS/EC2 区域 ID 和名称:

$ cat so_regions2.py
import boto3

region = "us-east-1"
print("Using region:", region)

ec2 = boto3.client("ec2", region_name=region)
ec2_responses = ec2.describe_regions()
ssm_client = boto3.client('ssm', region_name=region)
for resp in ec2_responses['Regions']:
region_id = resp['RegionName']
tmp = '/aws/service/global-infrastructure/regions/%s/longName' % region_id
ssm_response = ssm_client.get_parameter(Name = tmp)
region_name = ssm_response['Parameter']['Value']
print ("region_id:",region_id,"region_name:",region_name)

$ python3 so_regions2.py
Using region: us-east-1
region_id: eu-north-1 region_name: EU (Stockholm)
region_id: ap-south-1 region_name: Asia Pacific (Mumbai)
region_id: eu-west-3 region_name: EU (Paris)
region_id: eu-west-2 region_name: EU (London)
region_id: eu-west-1 region_name: EU (Ireland)
region_id: ap-northeast-2 region_name: Asia Pacific (Seoul)
region_id: ap-northeast-1 region_name: Asia Pacific (Tokyo)
region_id: sa-east-1 region_name: South America (Sao Paulo)
region_id: ca-central-1 region_name: Canada (Central)
region_id: ap-southeast-1 region_name: Asia Pacific (Singapore)
region_id: ap-southeast-2 region_name: Asia Pacific (Sydney)
region_id: eu-central-1 region_name: EU (Frankfurt)
region_id: us-east-1 region_name: US East (N. Virginia)
region_id: us-east-2 region_name: US East (Ohio)
region_id: us-west-1 region_name: US West (N. California)
region_id: us-west-2 region_name: US West (Oregon)
$

关于amazon-web-services - 现在有办法在 boto3 中获取 "AWS region names"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55909297/

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