【问题标题】:how to read a json file present in s3 bucket using boto3?如何使用 boto3 读取 s3 存储桶中存在的 json 文件?
【发布时间】:2020-05-06 02:28:27
【问题描述】:

我在 s3 存储桶中保存了一些 json 文件,我想使用 boto3 读取这些 json 文件的内容。 谁能建议怎么做?

【问题讨论】:

标签: python amazon-web-services


【解决方案1】:

这个问题可能与Already answered question right here.重复 也欢迎来到 SO,您需要在询问 questino 时发布您的示例代码,这表明您已经完成了研究并且无法找到任何有用的东西。看看这个。 How to ask a good question on Stack Overflow.

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

要从特定文件夹中读取,您可以试试这个

import boto3

s3 = boto3.resource('s3')
my_bucket = s3.Bucket('my_bucket_name')

for object_summary in my_bucket.objects.filter(Prefix="dir_name/"):
    print(object_summary.key)

学分 - M.Vanderlee

【讨论】:

  • 我尝试了同样的方法,但得到如下所示的错误:属性错误:'str object has no attribute 'óbjects'
  • 您能否编辑您的答案并显示您正在尝试的代码? @冠军
  • 我知道了如何读取 json 文件。感谢您的帮助。还有一个查询 - 如果不是整个存储桶,我只想从存储桶中的特定文件夹读取 json 文件,那么该怎么做是吗?
  • 别担心我的男人!你能接受我的回答吗?还有我定义key
  • 在我定义了key 的地方,试试obj.all。这应该可行。
猜你喜欢
  • 2017-09-29
  • 2016-07-12
  • 2019-03-22
  • 2018-11-07
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
  • 2018-04-29
  • 1970-01-01
相关资源
最近更新 更多