Python 如何从boto获取当前用户所在的区域?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37514810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the region of the current user from boto?
提问by Piotr Mazurek
Problem:
问题:
I'm trying to get the region of the authenticated user from boto3.
我正在尝试从 boto3 获取经过身份验证的用户的区域。
Use case:
用例:
I'm working on adding cache to https://github.com/pmazurek/aws-fuzzy-finder. I would prefer to cache the result on per-regionbasis.
我正在向https://github.com/pmazurek/aws-fuzzy-finder添加缓存。我更愿意在每个区域的基础上缓存结果。
This package uses boto to get user authentication data (keys and the region). The problem is the region is never passed explicitly by the user, its being taken from one of the many murky places that boto reads so I don't really have a way of getting it.
这个包使用 boto 来获取用户身份验证数据(密钥和区域)。问题是该区域从未被用户明确传递,它是从 boto 读取的众多模糊位置之一中获取的,所以我真的没有办法获得它。
I have tried searching through boto3 api and googling, but couldn't find anything like a get_region
or get_user_data
method. Is it possible?
我尝试通过 boto3 api 和谷歌搜索进行搜索,但找不到类似 aget_region
或get_user_data
方法的任何内容。是否可以?
回答by Frederic Henri
You should be able to read the region_name
from the session.Session
object like
您应该能够region_name
从session.Session
对象中读取
my_session = boto3.session.Session()
my_region = my_session.region_name
region_name
is basically defined as session.get_config_variable('region')
region_name
基本上定义为 session.get_config_variable('region')
回答by Steven Miller
Another option, if you are working with a boto3 client, is:
如果您使用 boto3 客户端,另一种选择是:
import boto3
client = boto3.client('s3') # example client, could be any
client.meta.region_name