Champion
Champion

Reputation: 51

How do I read a JSON file present in s3 bucket using boto3?

I have kept some JSON files in s3 bucket and I want to read the contents of those JSON files using boto3.

Upvotes: 2

Views: 5127

Answers (1)

Sahil
Sahil

Reputation: 1413

This question is probably a duplicate of Already answered question right here.

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

Upvotes: 7

Related Questions