连接S3,使用python第三方库boto3
import boto3
from boto3.session import Session
conf_s3 = {
"id":"qwqe",
"key":"fdss",
"url":"http://127.0.0.1:8000"
}
def get_stream(bucket, remote_file):
"""读取文件数据流"""
s3 = boto3.resource('s3',
aws_secret_access_key=conf_s3 ['key'],
aws_access_key_id=conf_s3 ['id'],
endpoint_url=conf_s3 ['url'])
obj = s3.Object(bucket, remote_file)
chunk_size = 1 * 1024 *1024
stream = obj.get().get('Body')
while True:
data = stream.read(chunk_size)
if not len(data):
raise StopIteration
yield data
def gclient(bucket, remote_file, byte_range=None):
"""获取文件对象以及文件流"""
session = Session(conf_s3 ['id'], conf_s3 ['key'])
s3 = session.client('s3', endpoint_url=conf_s3 ['url'])
if byte_range:
data = s3.get_object(Bucket=bucket, Key=remote_file, Range=byte_range)
else:
data = s3.get_object(Bucket=bucket, Key=remote_file)
return data
def fget(bucket, local_file, remote_file):
"""将桶里面的文件下载到本地文件指定的位置"""
session = Session(conf_s3 ['id'], conf_s3 ['key'])
s3 = session.client('s3', endpoint_url=conf_s3 ['url'])
with open(local_file, "wb") as f:
s3.download_fileobj(bucket, remote_file, f)
return True
def fput(bucket, local_file, remote_file):
"""文件上传到s3
Args:
bucket: string, s3桶
local_file: string, 本地文件路径
remote_file: string, s3文件路径
"""
session = Session(conf_s3 ['id'], conf_s3 ['key'])
s3 = session.client('s3', endpoint_url=conf_s3 ['url'])
with open(local_file, "rb") as f:
s3.upload_fileobj(f, bucket, remote_file)
return True
官方文档链接:https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.upload_fileobj
网友评论