How to compress the stream of data sent to client?

329 Views Asked by At

I am writing a Flask API endpoint that takes s3 URL as a parameter and sends the s3 object as response.

pseudoCode:

def stream_s3():
    while remaining_bytes>0:
       # ... logic to choose right byte range
       actual_bytes = aws.get_object(bucket, key, Range)
       yield Response(actual_bytes)

The content type is JSON, but some JSON can be more than 200 MB.

Question 1: How do I compress the bytes being sent? so that I can make it faster to receive the response.

Question 2: Some solutions suggest that I gzip the file and set the 'content-Encoding' as gzip. This basically needs the complete file to be downloaded in the server before gziping right?

0

There are 0 best solutions below