Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

python - Tell Flask to only store uploads in memory and not to use temporary files

I'm trying to bypass any local storage when receiving a file. According to the documentation, Flask keeps files in memory if it's "reasonable" otherwise stores them in a temporary location.

I only found a way to limit the size of the request in general with MAX_CONTENT_LENGTH. How do I control the threshold for the individual file size, or disable using temporary files completely?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Request._get_file_stream method is used to get file-like objects for storing file uploads. The default implementation uses SpooledTemporaryFile with a limit of 500 KiB, which keeps the data in memory before writing to a regular temporary file.

Subclass and override the method to always return BytesIO. Tell the Flask app to use that class instead of the default one.

from flask.wrappers import Request

class MemoryRequest(Request):
    def _get_file_stream(self, total_content_length, content_type, filename, content_length):
        return BytesIO()

app.request_class = MemoryRequest

You could also change the memory threshold by returning SpooledTemporaryFile(max_size=100_000_000, mode="rb+") (100 MB for example).

Memory is a much more limited shared resource than disk space, so it usually wouldn't be a good idea to store everything in memory. There should be no reason not to use temporary files.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...