如何使用boto3读取s3存储桶中存在的json文件? [英] how to read a json file present in s3 bucket using boto3?

查看:170
本文介绍了如何使用boto3读取s3存储桶中存在的json文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在s3存储桶中保留了一些json文件,我想使用boto3读取这些json文件的内容. 有人可以建议怎么做吗?

解决方案

此问题可能与如何在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)

I have kept some json files in s3 bucket and I want to read the contents of those json files using boto3. Can anyone suggest how to do it?

解决方案

This question is probably a duplicate of Already answered question right here. Also welcome to SO, you need to post your sample code as well when asking a questino, it shows that you've done your research and weren't able to find anything useful. Take a look at this. 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()

To read from a particular folder you can try this

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)

Credits - M.Vanderlee

这篇关于如何使用boto3读取s3存储桶中存在的json文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆