使用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 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    boto3提供了一种资源模型,该资源模型使诸如迭代对象之类的任务变得更加容易。不幸的是,StreamingBody不提供readlinereadlines

    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()
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看