Is there a way to treat a file in request.FILES
same as a disk file? I want to do something like this:
是否有一种处理请求文件的方法。文件与磁盘文件相同?我想做这样的事情:
with open(file_in_request, 'rb') as file:
#read data from that file
Only, I don't want to have to save it to disk first. I will be encrypting that file and save the encrypted file to disk instead.
只是,我不想先把它保存到磁盘上。我将对该文件进行加密,并将加密文件保存到磁盘上。
1 个解决方案
#1
1
One line answer by modifying FILE_UPLOAD_MAX_MEMORY_SIZE
Django variable to a large enough value to keep files in memory.
只需将FILE_UPLOAD_MAX_MEMORY_SIZE Django变量修改为足够大的值,以便将文件保存在内存中即可。
A more detailed answer:
更详细的回答:
By default, Django stores file with a size of 2.5 MB in memory and file larger than that size are stored in a temporary disk location.
默认情况下,Django在内存中存储大小为2.5 MB的文件,而大于这个大小的文件存储在临时磁盘位置。
The reason for this behavior is that keeping large files in memory can overwhelm your system. Read it here in docs.
这种行为的原因是将大文件保存在内存中会使系统崩溃。在文档中阅读。
In your case, if you know the max file size and are sure that it won't be large enough to fill up your computer memory you can modify the threshold value FILE_UPLOAD_MAX_MEMORY_SIZE
variable to a large enough value to make sure all the files are in memory.
在您的情况下,如果您知道最大文件大小,并且确定它不会足够大,以填充您的计算机内存,那么您可以将阈值FILE_UPLOAD_MAX_MEMORY_SIZE变量修改为足够大的值,以确保所有文件都在内存中。
From the docs:
从文档:
The maximum size (in bytes) that an upload will be before it gets streamed to the file system. See Managing files for details
上传至文件系统之前的最大大小(以字节为单位)。有关详细信息,请参阅管理文件。
#1
1
One line answer by modifying FILE_UPLOAD_MAX_MEMORY_SIZE
Django variable to a large enough value to keep files in memory.
只需将FILE_UPLOAD_MAX_MEMORY_SIZE Django变量修改为足够大的值,以便将文件保存在内存中即可。
A more detailed answer:
更详细的回答:
By default, Django stores file with a size of 2.5 MB in memory and file larger than that size are stored in a temporary disk location.
默认情况下,Django在内存中存储大小为2.5 MB的文件,而大于这个大小的文件存储在临时磁盘位置。
The reason for this behavior is that keeping large files in memory can overwhelm your system. Read it here in docs.
这种行为的原因是将大文件保存在内存中会使系统崩溃。在文档中阅读。
In your case, if you know the max file size and are sure that it won't be large enough to fill up your computer memory you can modify the threshold value FILE_UPLOAD_MAX_MEMORY_SIZE
variable to a large enough value to make sure all the files are in memory.
在您的情况下,如果您知道最大文件大小,并且确定它不会足够大,以填充您的计算机内存,那么您可以将阈值FILE_UPLOAD_MAX_MEMORY_SIZE变量修改为足够大的值,以确保所有文件都在内存中。
From the docs:
从文档:
The maximum size (in bytes) that an upload will be before it gets streamed to the file system. See Managing files for details
上传至文件系统之前的最大大小(以字节为单位)。有关详细信息,请参阅管理文件。