Stream data from Postgres to http request using Django StreamingHttpResponse
I would like to allow my users to download data from a Postgres DB, no matter the size of the requested data. For that reason, I would like to stream the data from the DB to the user.
I have seen that StreamingHttpResponse is useful to stream an HTTP response and there is a snippet in the Django documentation that shows how to stream a large CSV file.
I have also seen that psycopg2 has some streaming capabilities but I cannot find a way to "connect" the 2 streams.
I have tried something along the lines of:
class Echo:
"""An object that implements just the write method of the file-like
interface.
"""
def write(self, value):
"""Write the value by returning it, instead of storing in a buffer."""
return value
def stream(request):
sql_query = sql.SQL(
"""
COPY (select * from table) TO STDOUT WITH CSV HEADER
""")
# create the file like object
pseudo_buffer = Echo()
# I would like data to flow from the DB to HTTP response
with connection.cursor() as cursor:
return StreamingHttpResponse(cursor.copy_expert(sql_query, pseudo_buffer), content_type="text/csv", headers={"Content-Disposition": 'attachment; filename="somefilename.csv"'},)
I could make many small SQL requests and stream the data chunk by chunk using StreamingHttpResponse, but can I stream the data all the way from the DB to the client with only one DB request?