在Django中提供大文件(高负载) [英] Serving large files ( with high loads ) in Django

查看:184
本文介绍了在Django中提供大文件(高负载)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用一种方法来下载服务,但由于它不安全,我决定改变这一点。 (该方法是存储中原始文件的链接,但是风险在于所有连接的人都可以下载文件!)所以我现在通过我的意见提供文件,只有具有权限的用户才能下载该文件,但是我注意到服务器负载高,同时下载文件的请求也很多。这里是处理用户下载的代码的一部分(考虑文件是一个图像)

I've been using a method for serving downloads but since it was not secure i decided to change that . ( the method was a link to the original file in storage , but the risk was that everyone with the link could have downloaded the file ! ) so i now serve the file via my views , that way only users with permission can download the file , but i'm noticing a high load on server while there is many simultaneous download requests for the files. here's part of my code that handles downloads for users ( Consider the file is an image )

    image = Image.open ("the path to file")
    response = HttpResponse(mimetype = 'image/png' )
    response['Content-Disposition'] = 'attachment: filename=%s.png' % filename
    image.save(response , "png")
    return response  

有没有更好的方法在保持安全性和降低服务器端负载的同时提供文件?
提前感谢:)

is there any better ways for serving files while keeping the security and lowering server side load ? thanks in advance :)

推荐答案

您的打开图像将其加载到内存中,这是导致增加的原因在大量使用的负载。正如Martin发布的,真正的解决方案是直接提供文件。

Your opening of the image loads it in memory and this is what causes the increase in load under heavy use. As posted by Martin the real solution is to serve the file directly.

这是另一种方法,它会以块的形式流式传输文件,而无需将其加载到内存中。 >

Here is another approach, which will stream your file in chunks without loading it in memory.

import os
import mimetypes
from django.http import StreamingHttpResponse
from django.core.servers.basehttp import FileWrapper


def download_file(request):
   the_file = '/some/file/name.png'
   filename = os.path.basename(the_file)
   chunk_size = 8192
   response = StreamingHttpResponse(FileWrapper(open(the_file, 'rb'), chunk_size),
                           content_type=mimetypes.guess_type(the_file)[0])
   response['Content-Length'] = os.path.getsize(the_file)    
   response['Content-Disposition'] = "attachment; filename=%s" % filename
   return response

这篇关于在Django中提供大文件(高负载)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆