3

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?

Champion
  • 53
  • 2
  • 5
  • Does this answer your question? [Read file content from S3 bucket with boto3](https://stackoverflow.com/questions/36205481/read-file-content-from-s3-bucket-with-boto3) – Soumik Rakshit Jan 20 '20 at 05:15

1 Answers1

7

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

Sahil
  • 1,387
  • 14
  • 41
  • I tried the same but getting error as shown below:Attribute Error:'str object has no attribute 'óbjects' – Champion Jan 20 '20 at 05:42
  • Can you please edit your answer and show the code you're trying? @Champion – Sahil Jan 20 '20 at 05:48
  • I got it how to read json file.Thanks for your help.One more query- if instead of whole bucket,I want to read json files from only a specific folder in bucket,then how to do it? – Champion Jan 20 '20 at 06:37
  • No worries my man! Can you accept my answer? Also where I've defined `key`, – Sahil Jan 20 '20 at 06:38
  • Also where I've defined `key`, just try `obj.all`. This should probably work. – Sahil Jan 20 '20 at 06:44
  • Hey @Champion I have edited my answer to satisfy your new query, take a look at it now. – Sahil Jan 20 '20 at 06:46