本文介绍了连接到 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?
推荐答案
您可以创建一个会话:
import boto3
session = boto3.Session(
aws_access_key_id=settings.AWS_SERVER_PUBLIC_KEY,
aws_secret_access_key=settings.AWS_SERVER_SECRET_KEY,
)
然后使用该会话获取 S3 资源:
Then use that session to get an S3 resource:
s3 = session.resource('s3')
这篇关于连接到 boto3 S3 时如何指定凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!