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

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

问题描述

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

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天全站免登陆