gpt4 book ai didi

python - 在 python2 boto 中使用 aws 配置文件

转载 作者:太空狗 更新时间:2023-10-30 00:14:06 24 4
gpt4 key购买 nike

我希望我的 python 脚本(连接到 aws)使用来自 ~/.aws/credentials 的 aws 配置文件

所以,我有 ~/.aws/credentials 文件:

[default]
aws_access_key_id = XXX
aws_secret_access_key = AAA
region = eu-west-1

[test]
aws_access_key_id = WWW
aws_secret_access_key = ZZZ
region = eu-west-1

我的python代码

import boto.ec2

conn = boto.ec2.EC2Connection(profile_name='test')
print conn
print conn.get_all_instances()

输出是

EC2Connection:ec2.us-east-1.amazonaws.com
[]

所以,它似乎没有从配置中获取配置文件。地区不对。

env | grep -i aws <- returns nothing

我错过了什么?

谢谢。

=====更新=====

我也有一个 ~/.aws/config 文件,看起来像

⟩ cat config
[default]
output = text
region = eu-west-1

[profile test]
output = text
region = eu-west-1

=====更新=====

显然,它需要连接详细信息……但不包括区域。所以,如果我这样做

reg = next(x for x in boto.ec2.regions() if x.name=='eu-west-1')

conn = boto.ec2.EC2Connection(profile_name='test', region=reg)
print conn
print conn.get_all_instances()

我可以获得实例列表。

因此,问题是如何从 AWS config/credentials 获取默认“区域”。

最佳答案

credentials 文件应该仅用于凭证。如果您想为您的配置文件提供其他属性,您应该在 ~/.aws/config 文件中执行此操作。因此,在您的情况下,您将创建一个包含以下内容的配置文件:

[default]
region = eu-west-1

[test]
region = eu-west-1

关于python - 在 python2 boto 中使用 aws 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30330302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com