Read a file line by line from S3 using boto?

gignosko picture gignosko · Feb 19, 2015 · Viewed 57.8k times · Source

I have a csv file in S3 and I'm trying to read the header line to get the size (these files are created by our users so they could be almost any size). Is there a way to do this using boto? I thought maybe I could us a python BufferedReader, but I can't figure out how to open a stream from an S3 key. Any suggestions would be great. Thanks!

Answer

Michael Korbakov picture Michael Korbakov · Sep 28, 2016

You may find https://pypi.python.org/pypi/smart_open useful for your task.

From documentation:

for line in smart_open.smart_open('s3://mybucket/mykey.txt'):
    print line