• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Read Pickle File from AWS S3 Bucket Using Python

How to Read Pickle File from AWS S3 Bucket Using Python

February 2, 2023 Leave a Comment

To read a pickle file from ab AWS S3 Bucket using Python and pandas, you can use the boto3 package to access the S3 bucket. After accessing the S3 bucket, you can use the get_object() method to get the file by its name. Finally, you can use the pandas read_pickle() function on the Bytes representation of the file obtained by the io BytesIO() function.

import pandas as pd
import io
import boto3

s3c = boto3.client('s3', region_name="us-east-2",aws_access_key_id="YOUR AWS_ACCESS_KEY_ID",aws_secret_access_key="YOUR AWS_SECRET_ACCESS_KEY")
obj = s3c.get_object(Bucket="YOUR-BUCKET",Key="FILENAME")
df = pd.read_pickle(io.BytesIO(obj["Body"].read()))

When working with different datasets and file types, the ability to easily read and work with these different datasets is useful.

One such case is if you have data in an AWS S3 bucket and you want to read it into your Python program.

You can use the boto3 package which allows you to create, configure and manage AWS services.

With boto3, you can access the data in an AWS S3 bucket.

To start, you need to connect to AWS. This is done by first using the boto3 client function. You should pass your access key and secret access key here to authenticate.

Next, we want to get the object in question. To get the file, you can use the boto3 get_object() function and pass the bucket name and file name to the Bucket and Key parameters, respectively.

Now that we have the file, we can read it.

The get_object() function returns a dictionary with a few different pieces of information but what we care about is the “body” of the object.

The pandas read_pickle() function can read from a file path or a buffer. Therefore, to read the pickle file from the AWS S3 bucket, one solution would be to read the “body” of the object, convert it to bytes and then read it with read_pickle().

Below shows the entire code of how to read a pickle file from an AWS S3 bucket.

import pandas as pd
import io
import boto3

s3c = boto3.client('s3', region_name="us-east-2",aws_access_key_id="YOUR AWS_ACCESS_KEY_ID",aws_secret_access_key="YOUR AWS_SECRET_ACCESS_KEY")
obj = s3c.get_object(Bucket="YOUR-BUCKET",Key="FILENAME")
df = pd.read_pickle(io.BytesIO(obj["Body"].read()))

How to Read Excel Files and CSV Files from AWS S3 Buckets in Python

If you want to read Excel files or read csv files from an AWS S3 Bucket, then you can follow the same code structure as above.

read_excel() and read_csv() both allow you to pass a buffer, and so you can use io.BytesIO() to create the buffer.

Below shows an example of how you could read an Excel file from an AWS S3 bucket using Python and pandas.

import pandas as pd
import io
import boto3

s3c = boto3.client('s3', region_name="us-east-2",aws_access_key_id="YOUR AWS_ACCESS_KEY_ID",aws_secret_access_key="YOUR AWS_SECRET_ACCESS_KEY")
obj = s3c.get_object(Bucket="YOUR-BUCKET",Key="FILENAME")
df = pd.read_excel(io.BytesIO(obj["Body"].read()))

For reading a csv file, the code has the same structure.

import pandas as pd
import io
import boto3

s3c = boto3.client('s3', region_name="us-east-2",aws_access_key_id="YOUR AWS_ACCESS_KEY_ID",aws_secret_access_key="YOUR AWS_SECRET_ACCESS_KEY")
obj = s3c.get_object(Bucket="YOUR-BUCKET",Key="FILENAME")
df = pd.read_csv(io.BytesIO(obj["Body"].read()))

How to Write Pickle File to AWS S3 Bucket Using Python

If you want to write a pickle file to an AWS S3 Bucket, then you can do something similar as we have done above, but now you will use the boto3 put_object() function.

To write a pickle file from ab AWS S3 Bucket using Python and pandas, you can use the boto3 package to access the S3 bucket.

After accessing the S3 bucket, you need to create a file buffer with the io BytesIO() function.

Then, write the pickle file to the file buffer with the pandas to_pickle() function.

Finally, you can use the put_object() method to send the pickle file to a specified file location in the AWS S3 Bucket.

import pandas as pd
import io
import boto3

s3c = boto3.client('s3', region_name="us-east-2",aws_access_key_id="YOUR AWS_ACCESS_KEY_ID",aws_secret_access_key="YOUR AWS_SECRET_ACCESS_KEY")
pickle_buffer = io.BytesIO()
df.to_pickle(pickle_buffer)
s3c.put_object(Body=pickle_buffer.getvalue(),Bucket="YOUR-BUCKET",Key="FILENAME")

Hopefully this article has been useful for you to learn how to read a pickle file from an AWS S3 Bucket using Python and the pandas module.

Other Articles You'll Also Like:

  • 1.  Get Week Number from Date in Python
  • 2.  How to Use Python to Remove Zeros from List
  • 3.  Count Letters in Word in Python
  • 4.  How to Shutdown Computer with Python
  • 5.  Check if Variable is Tuple in Python
  • 6.  Python issuperset() Function – Check if Set is Superset of Another Set
  • 7.  Remove Trailing Zeros from String with rstrip() in Python
  • 8.  How to Read CSV File from AWS S3 Bucket Using Python
  • 9.  How to Write Excel File to AWS S3 Bucket Using Python
  • 10.  How to Capitalize the First Letter of Every Word in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy