Read a CSV file from S3 without saving it to the disk
The snippet can be accessed without any authentication.
Authored by
Bruno Lavoie
Edited
read-s3.py 741 B
# src.py
import boto3
import tempfile
import csv
import io
BUCKET_NAME = "foo-bucket"
OBJECT_NAME = "foo-file.csv"
s3 = boto3.client("s3")
# 'w+b' allows both reading from and writing to the file.
with tempfile.NamedTemporaryFile("w+b") as f:
s3.download_fileobj(BUCKET_NAME, OBJECT_NAME, f)
# This sets the file handle back to the beginning of the file.
# Without this, the loaded file will show no content.
f.seek(0)
# Here, 'io.TextIOWrapper' is converting the binary content of
# the file to be compatible with text content.
csv_reader = csv.DictReader(
io.TextIOWrapper(f, encoding="utf-8"),
)
# Now you're good to go.
for row in csv_reader:
# ... do processing
...
Please register or sign in to comment