Skip to content

Instantly share code, notes, and snippets.

@robsmith1776
Created January 25, 2024 20:53
Show Gist options
  • Save robsmith1776/df9c1d69c5bd121003ef1a8e87bf5e01 to your computer and use it in GitHub Desktop.
Save robsmith1776/df9c1d69c5bd121003ef1a8e87bf5e01 to your computer and use it in GitHub Desktop.
streamtoS3.py
import io
import psycopg2
import boto3
resource = boto3.resource('s3')
conn = psycopg2.connect(dbname=db, user=user, password=pw, host=host)
cur = conn.cursor()
def copyFun(bucket, select_query, filename):
query = f"""COPY {select_query} TO STDIN \
WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER TRUE)"""
file = io.StringIO()
cur.copy_expert(query, file)
resource.Object(bucket, f'{filename}.csv').put(Body=file.getvalue())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment