使用 boto3 从 S3 存储桶中读取文件内容

新手上路,请多包涵

我通过执行读取 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() 。什么是最好的方法?

原文由 mar tin 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.9k
2 个回答

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

原文由 Jordon Phillips 发布,翻译遵循 CC BY-SA 3.0 许可协议

使用客户端而不是资源:

 s3 = boto3.client('s3')
bucket='bucket_name'
result = s3.list_objects(Bucket = bucket, Prefix='/something/')
for o in result.get('Contents'):
    data = s3.get_object(Bucket=bucket, Key=o.get('Key'))
    contents = data['Body'].read()
    print(contents.decode("utf-8"))

原文由 Climbs_lika_Spyder 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题