Skip to content
Snippets Groups Projects

Read a CSV file from S3 without saving it to the disk

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Bruno Lavoie
    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
            ...
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment