Read Content From an S3 Bucket

Read Content From an S3 Bucket

It may not be obvious at first as to what the best method is to read the contents of a file that resides within an S3 bucket. If your attempts at this were anything like mine then you would have spent lots of time looking at the Boto3 S3 resource, and its various methods, only to not get any real results. The Boto3 SDK is tremendously powerful but very large and easy to get lost in.

This post will detail how to read the contents of a file that resides within an S3 bucket.

Before you begin:

  • Have the AWS Client installed and configured
  • Have the Boto3 client installed
  • Have an S3 Bucket with a file within it

The Boto3 Resource

To query AWS S3 and get our file we first need to import boto3 and specify the Boto3 resource we will be using.

We are making use of the boto3 resource as it is much simpler and higher Lebel than the boto3 client and satisfies our requirements.

Now we have our resource we are ready to query s3 and get our file. For this we will use the s3.Object() method, specifying the 2 keyword arguments, ‘bucket_name” and ‘key’.

Now we have the s3 Object within s3_file we can query it further and get what we need. If we call the get() method we get a nice dictionary returned as shown below.

Getting the data

Now we have the resource and the method to use, we are ready to read the contents of our file. For this we will use the read() method on the Body property of our dictionary. This will return it to us encoded so, finally we will decode it as ‘utf-8’.

This provides us with the contents of the file:

‘Hello Blog Readers\nThis is how we get our files(s) from S3\nUsing Boto3!’

Putting it all together

Now we have all the parts and are able to query S3 and return the contents of our file, we are ready to put it all together, turning the lines into a reusable definition as per the below example.

This code now prints the following:

Hello Blog Readers
This is how we get our files(s) from S3
Using Boto3!

Conclusion

This blog showed us how to query the Boto3 S3 resource to return the contents of a file (key). Hopefully this was helpful to you.

Adam

Leave a Reply

Your email address will not be published. Required fields are marked *