Jon*_*ice 30 python boto amazon-web-services
通过管理多个Amazon Web Services(AWS)帐户的最佳方式是什么boto?
我熟悉BotoConfig文件,我正在使用它.但是每个文件只描述一个帐户......而且我的工作不仅仅是一个组织.出于所有常见的法律,财务和安全原因,这些帐户不能混合使用.
目前我在boto每个帐户使用一个配置文件.例如:
~/.boto 默认帐户~/.boto_clowncollege 为"clowncollege"帐户~/.boto_razorassoc 为"razorassoc"帐户~/.boto_xyz 为"xyz"帐户然后像:
def boto_config_path(account=None):
    """
    Given an account name, return the path to the corresponding boto
    configuration file. If no account given, return the default config file.
    """
    path = '~/.boto' + ('_' + account if account else '')
    clean_path = os.path.abspath(os.path.expanduser(path))
    if os.path.isfile(clean_path):
        return clean_path
    else:
        errmsg = "cannot find boto config file {} for {}".format(clean_path, account)
        raise ValueError(errmsg)
def aws_credentials(account=None):
    """
    Return a tuple of AWS credentials (access key id and secret access key) for
    the given account.
    """
    try:
        cfg = INIConfig(open(boto_config_path(account)))
        return ( cfg.Credentials.aws_access_key_id, cfg.Credentials.aws_secret_access_key )
    except Exception:
        raise
conn = EC2Connection(*aws_credentials('razorassoc'))
好,坏,还是无动于衷?建议的改进?
Jan*_*sky 68
更新2015-02-06,通过以下顶部更正2015-03-19
从boto 2.29开始,有一种新的简单方法可以共享BOTO和AWS CLI凭证,如Mike Garnaat在AWS SDK中管理凭据的新标准化方法所述
目的是:
创建文件~/.aws/credentials(Mac/Linux)或%USERPROFILE%\.aws\credentials(Windwos),如下所示:
[default]
aws_access_key_id = AxxxA
aws_secret_access_key = Zxxxr
region = eu-west-1
[jekyl]
aws_access_key_id = AxxxA
aws_secret_access_key = Zxxxr
region = eu-west-1
[hyde]
aws_access_key_id = AxxxZ
aws_secret_access_key = CxxxZ
region = eu-west-1
从现在开始,您可以使用以下代码:
import boto
con = boto.connect_s3()
AWS_PROFILEenv 设置的显式配置文件.VAR(这是我最喜欢的选项,保持配置文件名称不在代码中,仍然让我的应用程序的部署者有机会选择特定的配置文件)
$ export AWS_PROFILE=jekyl
并保持您的代码像以前一样简单:
import boto
con = boto.connect_s3()
import boto
con = boto.connect_s3(profile_name="jekyl")
这是您通常需要做的所有事情
boto问题#2292中描述了选择适当凭据的逻辑如下:
从最高优先级到最低优先级的加载顺序:
1.直接从代码传递
密钥/秘密的环境变量
配置文件的环境变量
共享凭证文件显式配置文件
共享凭据文件默认配置文件
配置文件显式配置文件
配置文件凭据部分
从代码传递的配置文件会覆盖环境变量中的任何集合.
为了保持简洁,最好去掉旧方法,所以删除任何旧样式文件(如~/.aws/config或~/.boto),BOTO_CONFIG如果设置了取消设置环境varialbe ,也可能取消此变量指向的文件.
这真的是boto> = 2.29.0
注意:不要尝试通过env.variable(例如AWS_CONFIG_FILE)控制配置文件的位置,它不能按预期工作.
以下描述仅适用于那些无法升级到2.29.0或更高版本的用户
从boto 2.24.0开始,有一个叫做的功能 profile_name
在您的~/.boto文件中,您已经拥有[凭据]部分,这将作为后备选项,然后[个人资料]部分用于不同的配置文件:
[Credentials]
aws_access_key_id = AxxxA
aws_secret_access_key = Zxxxr
[profile jekyl]
aws_access_key_id = AxxxA
aws_secret_access_key = Zxxxr
[profile hyde]
aws_access_key_id = AxxxZ
aws_secret_access_key = CxxxZ
然后,在创建连接时,使用以下方式:
import boto
con = boto.connect_s3(profile_name="jekyl")
请注意,此功能自boto 2.24.0起可用.
教程在这里http://docs.pythonboto.org/en/latest/boto_config_tut.html?highlight=profile
甚至有一些关于使用密钥环的注意事项,但我会先习惯这个配置文件的东西,这是我梦寐以求的几年.
AWSCLI成为非常棒的工具.由于配置文件的格式几乎相同,我按以下方式使用它:
~/.aws/config文件保留为AWSCLI创建的(这是默认位置)[default]并将其重命名为[Credentials](在内部保留相同的值).BOTO_CONFIG变量设置为指向此~/.aws/config文件.然后~/.boto将成为`〜/ .aws/config,其中包含以下内容:
[default]
aws_access_key_id = AxxxA
aws_secret_access_key = Zxxxr
[Credentials]
aws_access_key_id = AxxxA
aws_secret_access_key = Zxxxr
[profile jekyl]
aws_access_key_id = AxxxA
aws_secret_access_key = Zxxxr
[profile hyde]
aws_access_key_id = AxxxZ
aws_secret_access_key = CxxxZ
通过这种方式,它可以共享AWSCLI和boto,包括配置文件.
将来,boto将提供更好的工具来帮助您管理多个凭据,但目前,有一些环境变量可能有所帮助.
首先,您可以将BOTO_CONFIG设置为指向要使用的boto配置文件,它将覆盖在正常位置找到的任何配置文件.
其次,您可以将BOTO_PATH设置为以冒号分隔的位置列表以查找boto配置文件,它将在正常搜索位置之前首先搜索该位置.
这些都没有给你你想要的东西,但它可以使用更少的代码更容易完成.
如果您有关于如何在博托工作的想法,请告诉我!
请考虑使用ConfigParser模块并在.boto文件中为每个帐户创建一个部分,而不是创建一堆单独的boto配置文件.
您的.boto文件可能看起来像这样
#Contents of ~/.boto
[clown-college]
aws_access_key_id = 123sesamestreet
aws_secret_access_key = 678idsaf567ujd
[razor-assoc]
aws_access_key_id = 437piedmont
aws_secret_access_key = 997567ujdfs
在您的python代码中,使用ConfigParser为您要使用的帐户加载适当的访问密钥.
import ConfigParser
from os.path import expanduser
########## BEGIN MAIN ##############
# get the path to the user's homedir
user_home = expanduser("~")
#load their .boto config file
config = ConfigParser.ConfigParser()
config.read([str(user_home + "/.boto")])
#get the keypair for ClownCollege
print config.get('clown-college', 'aws_access_key_id')
print config.get('clown-college', 'aws_secret_access_key')
print config.get('razor-assoc', 'aws_access_key_id')
print config.get('razor-assoc', 'aws_secret_access_key')
这可以包含在一个函数中,以便在您的boto代码中使用,以便轻松设置正确的帐户.
| 归档时间: | 
 | 
| 查看次数: | 38205 次 | 
| 最近记录: |