python连接AWS S3

直接登陆

假如想在python代码中直接用用户名密码登录AWS S3(其实就是AWS IAM USER信息登陆),其中的一个方法是获取一个session进行连接

from boto3.session import Session
session = Session(aws_access_key_id='mycount', aws_secret_access_key='mypass', region_name='cn-north-1')
#获取s3连接的session
s3 = session.resource('s3')
#列处所有的bucket 
for bucket in s3.buckets.all():
    print('bucket name:%s'%bucket.name)

 

另外,第二种连接方式:

import boto3

client = boto3.client(
    's3',
    aws_access_key_id=key_id,
    aws_secret_access_key=secret_key,
    region_name='cn-northwest-1'
)

with open(file, "rb") as f:
    client.upload_fileobj(f, "存储桶名", Key=key_value)

 

对于所有的连接方式可参考官方文档:https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html

posted @ 2020-08-26 10:56  neozheng  阅读(3360)  评论(0编辑  收藏  举报