连接到boto3 S3时如何指定凭据? [英] How to specify credentials when connecting to boto3 S3?

查看:217
本文介绍了连接到boto3 S3时如何指定凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

然后我可以使用S3执行操作(在我的情况下,是从存储桶中删除对象).

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

在boto3中,我发现的所有示例都是这样:

With boto3 all the examples I found are such:

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

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

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

如何使用boto3指定凭据?

How can I specify credentials with boto3?

推荐答案

您可以创建然后使用该会话获取S3资源:

Then use that session to get an S3 resource:

s3 = session.resource('s3')

这篇关于连接到boto3 S3时如何指定凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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