How to get the region of the current user from boto?

独自空忆成欢 提交于 2019-12-21 03:13:13

问题


Problem:

I'm trying to get the region of the authenticated user from 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-region basis.

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.

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?


回答1:


You should be able to read the region_name from the session.Session object like

my_session = boto3.session.Session()
my_region = my_session.region_name

region_name is basically defined as session.get_config_variable('region')




回答2:


Another option, if you are working with a boto3 client, is:

import boto3
client = boto3.client('s3') # example client, could be any
client.meta.region_name


来源:https://stackoverflow.com/questions/37514810/how-to-get-the-region-of-the-current-user-from-boto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!