如何使用 boto3 读取 s3 存储桶中存在的 JSON 文件?

2024-02-05

我在 s3 存储桶中保存了一些 JSON 文件,我想使用 boto3 读取这些 JSON 文件的内容。


这个问题可能是重复的已经在这里回答了问题。 https://stackoverflow.com/questions/36205481/read-file-content-from-s3-bucket-with-boto3

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)

学分 -范德利 https://stackoverflow.com/questions/27292145/python-boto-list-contents-of-specific-dir-in-bucket

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 boto3 读取 s3 存储桶中存在的 JSON 文件? 的相关文章

随机推荐