使用boto3连接到CloudFront时如何选择AWS配置文件 [英] How to choose an AWS profile when using boto3 to connect to CloudFront

查看:237
本文介绍了使用boto3连接到CloudFront时如何选择AWS配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Boto 3 python库,并想连接到AWS CloudFront. 我需要指定正确的AWS Profile(AWS凭证),但是在查看官方文档时,我看不到指定它的方法.

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.

我正在使用以下代码初始化客户端: client = boto3.client('cloudfront')

I am initializing the client using the code: 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.

推荐答案

我认为文档在公开如何执行此操作方面并不出色.一段时间以来,它一直是受支持的功能,并且在拉动请求.

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 different ways to do this:

选项A)使用个人资料创建一个新会话

Option A) Create a new session with the profile

    dev = boto3.session.Session(profile_name='dev')

选项B)更改代码中默认会话的配置文件

Option B) Change the profile of the default session in code

    boto3.setup_default_session(profile_name='dev')

选项C)使用环境变量更改默认会话的配置文件

Option C) Change the profile of the default session with an environment variable

    $ AWS_PROFILE=dev ipython
    >>> import boto3
    >>> s3dev = boto3.resource('s3')

这篇关于使用boto3连接到CloudFront时如何选择AWS配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆