boto3是否等同于boto.utils.get_instance_metadata()? [英] boto3 equivalent to boto.utils.get_instance_metadata()?
问题描述
在常规boto 2.38中,我曾经通过boto的
In regular boto 2.38 I used to access instance metadata (e.g. get current stack-name), through boto's
boto.utils.get_instance_metadata()
boto3中是否有一个等效项,还是我需要转到下级直接http地址以获取有关正在运行的实例的元数据?
Is there an equivalent in boto3, or do I need to go to the down level direct http address to fetch metadata about the running instance?
推荐答案
不,仍然没有boto3的同等功能,我自己就弥补了这一差距.
他们对此 https://github.com/boto/boto3/issues/313具有开放功能要求引用了这个问题.
Nope, still no equivalent in boto3, just hit this gap myself.
They have an open feature request for this https://github.com/boto/boto3/issues/313 that references this question.
关于解决方法,
您可以继续使用boto.utils
或使用urllib
/urllib2
手动执行HTTP请求,即.
As for workarounds,
you can continue to use boto.utils
or use urllib
/urllib2
to do the HTTP requests manually ie.
# Python2
import urllib2
instanceid = urllib2.urlopen('http://169.254.169.254/latest/meta-data/instance-id').read()
# Python3
import urllib.request
instanceid = urllib.request.urlopen('http://169.254.169.254/latest/meta-data/instance-id').read().decode()
请参见最快的HTTP GET方法中有关urllib和 http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-instance-metadata.html#instancedata-data-categories 用于元数据服务的URI结构.
see What is the quickest way to HTTP GET in Python? for a quick intro on urllib and http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-instance-metadata.html#instancedata-data-categories for the URI structure of the metadata service.
这篇关于boto3是否等同于boto.utils.get_instance_metadata()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!