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

查看:28
本文介绍了在 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天全站免登陆