Last active
April 28, 2021 01:11
-
-
Save onelharrison/20689c9d11da48e0bd28f40d50fbb194 to your computer and use it in GitHub Desktop.
Demo script for reading a CSV file from S3 into a pandas data frame using the boto3 library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Demo script for reading a CSV file from S3 into a pandas data frame using the boto3 library | |
""" | |
import os | |
import boto3 | |
import pandas as pd | |
AWS_S3_BUCKET = os.getenv("AWS_S3_BUCKET") | |
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID") | |
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY") | |
AWS_SESSION_TOKEN = os.getenv("AWS_SESSION_TOKEN") | |
s3_client = boto3.client( | |
"s3", | |
aws_access_key_id=AWS_ACCESS_KEY_ID, | |
aws_secret_access_key=AWS_SECRET_ACCESS_KEY, | |
aws_session_token=AWS_SESSION_TOKEN, | |
) | |
response = s3_client.get_object(Bucket=AWS_S3_BUCKET, Key="files/books.csv") | |
status = response.get("ResponseMetadata", {}).get("HTTPStatusCode") | |
if status == 200: | |
print(f"Successful S3 get_object response. Status - {status}") | |
books_df = pd.read_csv(response.get("Body")) | |
print(books_df) | |
else: | |
print(f"Unsuccessful S3 get_object response. Status - {status}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment