相关疑难解决方法(0)

在Django中提供大文件(高负载)

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

    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  
Run Code Online (Sandbox Code Playgroud)

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

python django permissions download

30
推荐指数
2
解决办法
3万
查看次数

Django - 收到来自外部网站的流请求

Django如何用于从外部API获取数据,由用户请求触发,并在请求周期中直接将其流回,而无需(或使用渐进/最小)内存使用?

背景

作为与外部托管的微服务连接的短期解决方案,需要将用户可访问性(基于Django应用程序的身份验证系统)限制为未经身份验证的API.以前的开发人员在Javascript中暴露了这些外部IP,我们需要一个解决方案来让他们脱离公众的视线.

要求

  • 我们不一定会使用请求库,并且可以使用任何其他库,如果它可以帮助加快响应时间.
  • 来自外部API的响应可能有点大(5-10MB)并且能够缩短请求周期(通过Ajax> Django>外部API> Django>用户的用户请求)是至关重要的.

这可能吗?如果是这样,你能建议一个方法吗?

from django.shortcuts import Http404, HttpResponse
import requests

def api_gateway_portal(request, path=''):
    # Determine whether to grant access

    # If so, fetch and return data
    r = requests.get('http://some.ip.address/%s?api_key=12345678901234567890' % (path,))

    # Return as JSON 
    response = HttpResponse(r.content, content_type='application/json')
    response['Content-Length'] = len(r.content)

    return response
Run Code Online (Sandbox Code Playgroud)

请注意 - 我完全清楚这是一个糟糕的长期解决方案,但在完成新的外部身份验证系统之前,必须进行短期的演示.

python django python-requests

4
推荐指数
1
解决办法
1305
查看次数

Django Filewrapper 内存错误服务大文件,如何流式传输

我有这样的代码:

@login_required
def download_file(request):
    content_type = "application/octet-stream"
    download_name = os.path.join(DATA_ROOT, "video.avi")

    with open(download_name, "rb") as f:
        wrapper = FileWrapper(f, 8192)
        response = HttpResponse(wrapper, content_type=content_type)
    response['Content-Disposition'] = 'attachment; filename=blabla.avi'
    response['Content-Length'] = os.path.getsize(download_name)
    # response['Content-Length'] = _file.size
    return response
Run Code Online (Sandbox Code Playgroud)

似乎它有效。但是,如果我下载更大的文件(例如~600MB),我的内存消耗会增加 600MB。经过几次这样的下载后,我的服务器抛出:

内部服务器错误:/download/ 回溯(最近一次调用最后一次):
文件“/home/matous/.local/lib/python3.5/site-packages/django/core/handlers/exception.py”,第 35 行,在内部响应 = get_response(request) 文件“/home/matous/. local/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response response = self.process_exception_by_middleware(e, request) File "/home/matous/.local/lib/ python3.5/site-packages/django/core/handlers/base.py”,第 126 行,在 _get_response 响应 =wrapped_callback(request, *callback_args, **callback_kwargs) 文件“/home/matous/.local/lib/python3 .5/site-packages/django/contrib/auth/decorators.py”,第 21 行,在 _wrapped_view 中返回 view_func(request, *args, **kwargs) 文件“/media/matous/89104d3d-fa52-4b14-9c5d- 9ec54ceebebb/home/matous/phd/emoapp/emoapp/mainapp/views.py",第 118 行,在 download_file …

python django streaming file download

1
推荐指数
1
解决办法
2576
查看次数