使用boto3从S3存储桶读取文件内容 [英] Read file content from S3 bucket with boto3

查看:1111
本文介绍了使用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不提供readlinereadlines.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆