Last active
August 19, 2021 03:16
-
-
Save onelharrison/83b428803d605c7ddf312de8f2349e0f to your computer and use it in GitHub Desktop.
Demo script for writing a pandas data frame to a CSV file on S3 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 writing a pandas data frame to a CSV file on S3 using the boto3 library | |
""" | |
import io | |
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, | |
) | |
books_df = pd.DataFrame( | |
data={"Title": ["Book I", "Book II", "Book III"], "Price": [56.6, 59.87, 74.54]}, | |
columns=["Title", "Price"], | |
) | |
with io.StringIO() as csv_buffer: | |
books_df.to_csv(csv_buffer, index=False) | |
response = s3_client.put_object( | |
Bucket=AWS_S3_BUCKET, Key="files/books.csv", Body=csv_buffer.getvalue() | |
) | |
status = response.get("ResponseMetadata", {}).get("HTTPStatusCode") | |
if status == 200: | |
print(f"Successful S3 put_object response. Status - {status}") | |
else: | |
print(f"Unsuccessful S3 put_object response. Status - {status}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment