【发布时间】:2020-05-06 02:28:27
【问题描述】:
我在 s3 存储桶中保存了一些 json 文件,我想使用 boto3 读取这些 json 文件的内容。 谁能建议怎么做?
【问题讨论】:
-
这能回答你的问题吗? Read file content from S3 bucket with boto3
标签: python amazon-web-services
我在 s3 存储桶中保存了一些 json 文件,我想使用 boto3 读取这些 json 文件的内容。 谁能建议怎么做?
【问题讨论】:
标签: python amazon-web-services
这个问题可能与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
【讨论】:
key,
key 的地方,试试obj.all。这应该可行。