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 InvalidAccessKeyId
error.
我无法指定我的凭据,因此所有尝试都因InvalidAccessKeyId错误而失败。
How can I specify credentials with boto3?
如何使用boto3指定凭据?
2 个解决方案
#1
23
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')
#2
9
You can get a client
with new session directly like below.
您可以直接获得具有新会话的客户端,如下所示。
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
)
#1
23
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')
#2
9
You can get a client
with new session directly like below.
您可以直接获得具有新会话的客户端,如下所示。
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
)