Python3 Flask upload file in server memory Python3 Flask upload file in server memory flask flask

Python3 Flask upload file in server memory


Incoming file uploads are indeed presented as FileStorage objects. However, this does not necessarily mean that an actual physical file is involved.

When parsing file objects, Werkzeug uses the stream_factory() callable to produce a file object. The default implementation only creates an actual physical file for file sizes of 500kb and over, to avoid eating up memory.

For smaller files an in-memory file object is used instead.

I'd not tamper with this arrangement; as it works right now the issue is handled transparently and your harddisk is only involved when the file uploads would otherwise tax your memory too much.

Rather, I'd alter that function to not require a filename and / or accept a file object.

If your function can only take a path or the contained data as a string, you can see if you need to read the file by introspecting the underlying .stream attribute:

from werkzeug._compat import BytesIOfilename = data = Noneif file_upload.filename is None:    data = file_upload.read()  # in-memory stream, so read it out.else:    filename = file_upload.filename


You can store uploaded files to tmpfs. This way they will still be regular files than can be opened with open().