Read and write to/from s3 using python — boto3 and pandas (s3fs)!

!pip install -m boto3
!pip install -m pandas "s3fs<=0.4"
import io
import os
import boto3
import pandas as pd
AWS_S3_BUCKET = "samplebucket1303"
AWS_ACCESS_KEY_ID = "accesskey"
AWS_SECRET_ACCESS_KEY = "secretkey"
s3_client = boto3.client(
"s3",
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
)
df = pd.read_csv("Language Detection.csv")
with io.StringIO() as csv_buffer:
df.to_csv(csv_buffer, index=False)
response = s3_client.put_object(
Bucket=AWS_S3_BUCKET, Key="Language_Detection_boto3.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}")
df.to_csv(
f"s3://{AWS_S3_BUCKET}/Language_Detection_s3fs.csv",
index=False,
storage_options={
"key": AWS_ACCESS_KEY_ID,
"secret": AWS_SECRET_ACCESS_KEY,
}
)
response = s3_client.get_object(Bucket=AWS_S3_BUCKET, Key="Language_Detection_boto3.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}")
df = pd.read_csv(
f"s3://{AWS_S3_BUCKET}/Language_Detection_s3fs.csv",
storage_options={
"key": AWS_ACCESS_KEY_ID,
"secret": AWS_SECRET_ACCESS_KEY,
}
)
df.head()

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store