Is it possible to loop through Amazon S3 bucket and count the number of lines in its file/key using Python?

Question:

Is it possible to loop through the file/key in Amazon S3 bucket, read the contents and count the number of lines using Python?

For Example:

I need to loop through the file “test.txt” and count the number of line in the raw file.

Sample Code:

Answer:

Using boto3 you can do the following:

If you want to do this for all objects in a bucket, you can use the following code snippet:

Here is the reference to boto3 documentation for more functionality: http://boto3.readthedocs.io/en/latest/reference/services/s3.html#object

Update: (Using boto 2)

Leave a Reply