Python 连接到 boto3 S3 时如何指定凭据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45981950/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:24:32  来源:igfitidea点击:

How to specify credentials when connecting to boto3 S3?

pythonamazon-web-servicesamazon-s3boto3

提问by Robert Brax

On boto I used to specify my credentials when connecting to S3 in such a way:

在 boto 上,我曾经在以这种方式连接到 S3 时指定我的凭据:

import boto
from boto.s3.connection import Key, S3Connection
S3 = S3Connection( settings.AWS_SERVER_PUBLIC_KEY, settings.AWS_SERVER_SECRET_KEY )

I could then use S3 to perform my operations (in my case deleting an object from a bucket).

然后我可以使用 S3 来执行我的操作(在我的例子中从存储桶中删除一个对象)。

With boto3 all the examples I found are such:

使用 boto3,我发现的所有示例都是这样的:

import boto3
S3 = boto3.resource( 's3' )
S3.Object( bucket_name, key_name ).delete()

I couldn't specify my credentials and thus all attempts fail with InvalidAccessKeyIderror.

我无法指定我的凭据,因此所有尝试都失败并显示InvalidAccessKeyId错误。

How can I specify credentials with boto3?

如何使用 boto3 指定凭据?

回答by Alasdair

You can create a session:

您可以创建一个会话

import boto3
session = boto3.Session(
    aws_access_key_id=settings.AWS_SERVER_PUBLIC_KEY,
    aws_secret_access_key=settings.AWS_SERVER_SECRET_KEY,
)

Then use that session to get an S3 resource:

然后使用该会话获取 S3 资源:

s3 = session.resource('s3')

回答by Rajez

You can get a clientwith new session directly like below.

你可以client像下面一样直接获得一个新的会话。

 s3_client = boto3.client('s3', 
                      aws_access_key_id=settings.AWS_SERVER_PUBLIC_KEY, 
                      aws_secret_access_key=settings.AWS_SERVER_SECRET_KEY, 
                      region_name=REGION_NAME
                      )

回答by x85ms16

I'd like expand on @JustAGuy's answer. The method I prefer is to use AWS CLIto create a config file. The reason is, with the config file, the CLIor the SDKwill automatically look for credentials in the ~/.awsfolder. And the good thing is that AWS CLIis written in python.

我想扩展@JustAGuy 的回答。我更喜欢的方法是使用AWS CLI创建配置文件。原因是,使用配置文件,CLISDK将自动在~/.aws文件夹中查找凭据。好消息是它AWS CLI是用 python 编写的。

You can get cli from pypi if you don't have it already. Here are the steps to get cli set up from terminal

如果你还没有 cli,你可以从 pypi 获取它。以下是从终端设置 cli 的步骤

$> pip install awscli  #can add user flag 
$> aws configure
AWS Access Key ID [****************ABCD]:[enter your key here]
AWS Secret Access Key [****************xyz]:[enter your secret key here]
Default region name [us-west-2]:[enter your region here]
Default output format [None]:

After this you can access botoand any of the api without having to specify keys (unless you want to use a different credentials).

在此之后,您可以访问boto任何 api,而无需指定密钥(除非您想使用不同的凭据)。

回答by DataDecay

This is older but placing this here for my reference too. boto3.resource is just implementing the default Session, you can pass through boto3.resource session details.

这是旧的,但也把它放在这里供我参考。boto3.resource 只是实现了默认的 Session,您可以通过 boto3.resource 会话详细信息。

Help on function resource in module boto3:

resource(*args, **kwargs)
    Create a resource service client by name using the default session.

    See :py:meth:`boto3.session.Session.resource`.

https://github.com/boto/boto3/blob/86392b5ca26da57ce6a776365a52d3cab8487d60/boto3/session.py#L265

https://github.com/boto/boto3/blob/86392b5ca26da57ce6a776365a52d3cab8487d60/boto3/session.py#L265

you can see that it just takes the same arguments as Boto3.Session

你可以看到它只需要与 Boto3.Session 相同的参数

import boto3
S3 = boto3.resource('s3', region_name='us-west-2', aws_access_key_id=settings.AWS_SERVER_PUBLIC_KEY, aws_secret_access_key=settings.AWS_SERVER_SECRET_KEY)
S3.Object( bucket_name, key_name ).delete()

回答by JustAGuy

There are numerous ways to store credentials while still using boto3.resource(). I'm using the AWS CLI method myself. It works perfectly.

有很多方法可以在仍然使用 boto3.resource() 的同时存储凭据。我自己正在使用 AWS CLI 方法。它完美地工作。

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html?fbclid=IwAR2LlrS4O2gYH6xAF4QDVIH2Q2tzfF_VZ6loM3XfXsPAOR4qA-pX_qAILys

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html?fbclid=IwAR2LlrS4O2gYH6xAF4QDVIH2Q2tzfF_VZ6loM3XfXsPAOR4qA-pX_qAILys