使用 boto3 从 S3 存储桶读取文件内容 [英] Read file content from S3 bucket with boto3
本文介绍了使用 boto3 从 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()
.最好的方法是什么?
Now, I need to get the actual content of the file, similarly to a open(filename).readlines()
. What is the best way?
推荐答案
boto3 提供了一种资源模型,可以使遍历对象等任务变得更容易.不幸的是,StreamingBody 不提供 readline
或 readlines
.
boto3 offers a resource model that makes tasks like iterating through objects easier. Unfortunately, StreamingBody doesn't provide readline
or 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()
这篇关于使用 boto3 从 S3 存储桶读取文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文