在Boto 2中,我可以使用以下命令获取与域名domain关联的托管区域
r53_2 = boto.route53.connection.Route53Connection()
hz = r53_2.get_zone(domain)但在Boto 3中,corresponding API需要ID而不是域名
r53_3 = boto3.client('route53')
hz = r53_3.get_hosted_zone(id)我看不到任何从域名获取ID的方法,这是我唯一有权访问的域名。
如何使用Boto 3获取域名的托管区域?
发布于 2017-02-25 10:37:35
我现在不能测试它,但是你能使用.list_hosted_zones()吗?
您需要解析结果,但这只是一个开始。
RL
发布于 2020-05-08 01:22:54
list_hosted_zones_by_name有点误导人:它仍然返回一个列表,但首先列出包含DNSName的记录。注意:如果记录不存在,它将按字母顺序(我认为)返回下一条记录,因此获取列表中的第一条记录并不能保证它就是您想要的那条记录。以下是对我有效的方法:
import boto3, json
client = boto3.client('route53')
dnsName = 'example.com'
response = client.list_hosted_zones_by_name(
DNSName=dnsName,
MaxItems='1'
)
print(json.dumps(response, indent=4)) # inspect output
if ('HostedZones' in response.keys()
and len(response['HostedZones']) > 0
and response['HostedZones'][0]['Name'].startswith(dnsName)):
hostedZoneId = response['HostedZones'][0]['Id'].split('/')[2] # response comes back with /hostedzone/{HostedZoneId}
print('HostedZone {} found with Id {}'.format(dnsName, hostedZoneId))
else:
print('HostedZone not found: {}'.format(dnsName))发布于 2019-04-10 09:34:40
也许这个例子会有所帮助。
r53 = boto3.client('route53')
zones = r53.list_hosted_zones_by_name(DNSName=domain)
if not zones or len(zones['HostedZones']) == 0:
raise Exception("Could not find DNS zone to update")
zone_id = zones['HostedZones'][0]['Id']https://stackoverflow.com/questions/42446412
复制相似问题