read file from s3 python 1

read file from s3 python

boto3 offers a resource model that makes tasks like iterating through objects easier. Unfortunately, StreamingBody doesn't provide readline or readlines.

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()

Here is what the above code is Doing:
1. Create an S3 resource.
2. Get a bucket by name.
3. Get a list of objects for that bucket.
4. Iterate over the list of objects.
5. Get an object by key.
6. Read the object’s Body.

Similar Posts