如何在上传时不将整个文件加载到内存中
我正在使用Bottle来创建一个文件上传的接口。下面的代码可以把文件上传到一个目录,但我遇到了两个问题需要解决。一个是如何避免把整个文件加载到内存中,另一个是如何设置上传文件的最大大小。
有没有办法可以持续读取文件,并把读取到的内容写入文件,直到上传完成?upload.save(file_path, overwrite=False, chunk_size=1024)
这个函数似乎会把整个文件都加载到内存里。在教程中,他们提到使用.read()
是有风险的。
from bottle import Bottle, request, run, response, route, default_app, static_file
app = Bottle()
@route('/upload', method='POST')
def upload_file():
function_name = sys._getframe().f_code.co_name
try:
upload = request.files.get("upload_file")
if not upload:
return "Nothing to upload"
else:
#Get file_name and the extension
file_name, ext = os.path.splitext(upload.filename)
if ext in ('.exe', '.msi', '.py'):
return "File extension not allowed."
#Determine folder to save the upload
save_folder = "/tmp/{folder}".format(folder='external_files')
if not os.path.exists(save_folder):
os.makedirs(save_folder)
#Determine file_path
file_path = "{path}/{time_now}_{file}".\
format(path=save_folder, file=upload.filename, timestamp=time_now)
#Save the upload to file in chunks
upload.save(file_path, overwrite=False, chunk_size=1024)
return "File successfully saved {0}{1} to '{2}'.".format(file_name, ext, save_folder)
except KeyboardInterrupt:
logger.info('%s: ' %(function_name), "Someone pressed CNRL + C")
except:
logger.error('%s: ' %(function_name), exc_info=True)
print("Exception occurred111. Location: %s" %(function_name))
finally:
pass
if __name__ == '__main__':
run(host="localhost", port=8080, reloader=True, debug=True)
else:
application = default_app()
我也尝试过使用file.write,但情况也是一样。文件被读取到内存中,导致机器卡住。
file_to_write = open("%s" %(output_file_path), "wb")
while True:
datachunk = upload.file.read(1024)
if not datachunk:
break
file_to_write.write(datachunk)
与此相关,我看到有一个属性MEMFILE_MAX,有几个SO帖子声称可以设置最大文件上传大小。我尝试设置了这个值,但似乎没有任何效果,因为所有文件无论大小都能上传。
请注意,我希望能够接收办公文档,这些文档可能是普通的文件扩展名,或者是带密码的压缩文件。
我使用的是Python3.4和Bottle 0.12.7
1 个回答
1
基本上,你想要在一个循环里调用 upload.read(1024)
。可以这样做(这个代码没有测试过):
with open(file_path, 'wb') as dest:
chunk = upload.read(1024)
while chunk:
dest.write(chunk)
chunk = upload.read(1024)
(不要对 upload
使用 open
,因为它已经为你打开了。)
这个SO的回答里有更多关于如何读取大文件而不一次性加载全部内容的例子。