Python 使用 boto3 连接到 CloudFront 时如何选择 AWS 配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33378422/
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 choose an AWS profile when using boto3 to connect to CloudFront
提问by Nader A. Jabbar
I am using the Boto 3 python library, and want to connect to AWS CloudFront. I need to specify the correct AWS Profile (AWS Credentials), but looking at the official documentation, I see no way to specify it.
我正在使用 Boto 3 python 库,并且想要连接到 AWS CloudFront。我需要指定正确的 AWS 配置文件(AWS 凭证),但是查看官方文档,我看不到指定它的方法。
I am initializing the client using the code:
client = boto3.client('cloudfront')
我正在使用以下代码初始化客户端:
client = boto3.client('cloudfront')
However, this results in it using the default profile to connect. I couldn't find a method where I can specify which profile to use.
但是,这会导致它使用默认配置文件进行连接。我找不到可以指定要使用的配置文件的方法。
采纳答案by Jordon Phillips
I think the docs aren't wonderful at exposing how to do this. It has been a supported feature for some time, however, and there are some details in this pull request. So there are three ways to do this:
我认为文档在公开如何做到这一点方面并不出色。然而,它已经成为受支持的功能有一段时间了,并且在这个pull request 中有一些细节。所以有三种方法可以做到这一点:
a) Create a new session with the profile
a) 使用配置文件创建一个新会话
dev = boto3.session.Session(profile_name='dev')
b) Change the profile of the default session in code
b) 在代码中更改默认会话的配置文件
boto3.setup_default_session(profile_name='dev')
c) Change the profile of the default session with an environment variable
c) 使用环境变量更改默认会话的配置文件
$ AWS_PROFILE=dev ipython
>>> import boto3
>>> s3dev = boto3.resource('s3')
回答by asmaier
Do this to use a profile with name 'dev':
这样做以使用名为“dev”的配置文件:
session = boto3.session.Session(profile_name='dev')
s3 = session.resource('s3')
for bucket in s3.buckets.all():
print(bucket.name)
回答by mgig
This sectionof the boto3 documentation is helpful.
boto3 文档的这一部分很有帮助。
Here's what worked for me:
以下是对我有用的内容:
session = boto3.Session(profile_name='dev')
client = session.client('cloudfront')
回答by MrKulli
Just add profile to session configuration before client call.
boto3.session.Session(profile_name='YOUR_PROFILE_NAME').client('cloudwatch')
只需在客户端调用之前将配置文件添加到会话配置中。
boto3.session.Session(profile_name='YOUR_PROFILE_NAME').client('cloudwatch')