Reputation: 71
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?
Upvotes: 3
Views: 1245
Reputation: 127320
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.
Upvotes: 3