使用boto3从S3存储桶读取文件内容
发布于 2021-01-29 18:40:21
我这样做是读取了S3存储桶中的文件名
objs = boto3.client.list_objects(Bucket='my_bucket')
while 'Contents' in objs.keys():
objs_contents = objs['Contents']
for i in range(len(objs_contents)):
filename = objs_contents[i]['Key']
现在,我需要获取文件的实际内容,类似于open(filename).readlines()
。什么是最好的方法?
关注者
0
被浏览
149
1 个回答
-
boto3提供了一种资源模型,该资源模型使诸如迭代对象之类的任务变得更加容易。不幸的是,StreamingBody不提供
readline
或readlines
。s3 = boto3.resource('s3') bucket = s3.Bucket('test-bucket') # Iterates through all the objects, doing the pagination for you. Each obj # is an ObjectSummary, so it doesn't contain the body. You'll need to call # get to get the whole body. for obj in bucket.objects.all(): key = obj.key body = obj.get()['Body'].read()