Skip to content

Instantly share code, notes, and snippets.

@onelharrison
Last active August 19, 2021 03:16
Show Gist options
  • Save onelharrison/83b428803d605c7ddf312de8f2349e0f to your computer and use it in GitHub Desktop.
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
"""
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