flask-storage icon indicating copy to clipboard operation
flask-storage copied to clipboard

Solution for s3 storage + uwsgi

Open freesoul opened this issue 5 months ago • 0 comments

send_file(f , ...) in s3.py works by chance when no proxy/uwsgi is in front.

send_file expects a file path or file stream reader such as io.BytesIO, but the body reader coming from boto3 in s3.py will either give a timeout with uwsgi if the flag wsgi-disable-file-wrapper = true is not set, or a 200 but empty response if the flag is set.

The correct solution would be to set wsgi-disable-file-wrapper = true and to add a flask Response with a generator of data, such as:


    def serve(self, filename):

        # Open the S3 object
        with self.open(filename) as f:
            # Get the MIME type for the response
            mime_type = self.get_metadata(filename)['mime']
            
            # Define a generator function to yield chunks of data
            def generate():
                try:
                    while chunk := f.read(8192):  # Read in chunks of 8 KB
                        yield chunk
                finally:
                    f.close()  # Ensure the stream is closed

            # Return a Response object with the generator
            return Response(generate(), content_type=mime_type)

freesoul avatar Sep 04 '24 14:09 freesoul