Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read file content from S3 bucket with boto3

I read the filenames in my S3 bucket by doing

objs = boto3.client.list_objects(Bucket='my_bucket')     while 'Contents' in objs.keys():         objs_contents = objs['Contents']         for i in range(len(objs_contents)):             filename = objs_contents[i]['Key'] 

Now, I need to get the actual content of the file, similarly to a open(filename).readlines(). What is the best way?

like image 296
mar tin Avatar asked Mar 24 '16 16:03

mar tin


People also ask

How do I read an AWS S3 bucket?

In the Amazon S3 console, choose your S3 bucket, choose the file that you want to open or download, choose Actions, and then choose Open or Download. If you are downloading an object, specify where you want to save it. The procedure for saving the object depends on the browser and operating system that you are using.

Can I read S3 file without downloading?

Reading objects without downloading them Similarly, if you want to upload and read small pieces of textual data such as quotes, tweets, or news articles, you can do that using the S3 resource method put(), as demonstrated in the example below (Gist).


1 Answers

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() 
like image 65
Jordon Phillips Avatar answered Oct 10 '22 17:10

Jordon Phillips