简体   繁体   English

使用Python / Django上传大文件

[英]Uploading large files with Python/Django

I am wondering if there are any ramifications in uploading files that are roughly 4GB in size through a web app using Django/Python? 我想知道使用Django / Python通过Web应用程序上传大小约为4GB的文件是否有任何后果? I remember in the past streaming uploads using Java was the preferred method but does this still today or is it perfectly safe to do so with Django/Python? 我记得在过去使用Java的流式上传是首选的方法但是今天仍然这样做或者使用Django / Python这样做是否完全安全?

Django will by default, put uploaded file data into memory if it is less than 2.5MB. Django默认情况下,如果上传的文件数据小于2.5MB,则将其放入内存中。 Anything larger will be written to the server's /tmp directory and then copied across when the transfer completes. 更大的内容将写入服务器的/tmp目录,然后在传输完成时复制。 Many of Django's file upload settings can be customised, details are available in the documentation . 许多Django的文件上传设置都可以自定义, 文档中提供了详细信息。 You can also customise the file handling and you'll certainly want to do this. 您还可以自定义文件处理,您当然希望这样做。

Before we consider any technical constraints, uploading such large files with the browser will give the user a very poor experience. 在我们考虑任何技术限制之前,使用浏览器上传这些大文件会给用户带来非常糟糕的体验。 There is no feedback about how the transfer is going (although google chrome does display the upload status as a percentage) and no way to pause or resume transfers. 没有关于传输方式的反馈(尽管谷歌浏览器确实以百分比显示上传状态)并且无法暂停或恢复传输。

You are also likely to run into problems on the server. 您也可能在服务器上遇到问题。 Apart from the extremely long time that each thread will be taken with dealing with the streamed data, you have the time it takes for the system to copy the resulting file from /tmp to its correct location. 除了处理流数据每个线程的极长时间外,您还有时间让系统将生成的文件从/tmp复制到正确的位置。

Unless you are very confident that you can foresee any problem that the server might have with the uploads, I would suggest that this is a bad idea. 除非您非常有信心可以预见到服务器可能与上传有任何问题,否则我建议这是一个坏主意。 It's pretty hard to find any information on this via google and there do seem to be a lot of hits that describe problems with large file uploads. 很难通过谷歌找到这方面的任何信息,似乎有很多描述大文件上传问题的点击。

While Django is technically capable of receiving uploaded files this large, the very poor user experience and technical difficulties mean this may not be the best approach. 虽然Django在技术上能够接收这么大的上传文件,但非常糟糕的用户体验和技术难度意味着这可能不是最好的方法。 Have you considered using dedicated software to handle the file transfer? 您是否考虑过使用专用软件来处理文件传输?

The last answer covers it. 最后的答案涵盖了它。 We routinely upload 2.5mb+ (but usually not 4gb) 我们经常上传2.5mb +(但通常不是4gb)

adamnish link is correct, see this snippet (from his link to django docs ) regarding writing the file to disk, instead of having it in memory first: adamnish链接是正确的,请参阅此代码段(从他的链接到django docs )关于将文件写入磁盘,而不是先将其放入内存:

def handle_uploaded_file(f):
    with open('some/file/name.txt', 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

More info on the "chunks" call: https://docs.djangoproject.com/en/dev/ref/files/uploads/#django.core.files.uploadedfile.UploadedFile.chunks 有关“chunks”调用的更多信息: https//docs.djangoproject.com/en/dev/ref/files/uploads/#django.core.files.uploadedfile.UploadedFile.chunks

Page includes how to set "chunk" size, etc. 页面包括如何设置“块”大小等。

For future readers: To up the max filesize allowed with in memory storage set the following in your settings.py : 对于未来的读者:要在内存存储中允许最大文件大小 ,请在settings.py以下内容:

FILE_UPLOAD_MAX_MEMORY_SIZE = 5242880 # make it 5Mb instead of 2Mb

Of course this won't help you for 4Gb. 当然这对4Gb没有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM