3

我有一组静态文件(通过应用程序上传),如图像、视频等,需要提供给经过身份验证的用户(即他们的 cookie 在会话中注册为经过身份验证)。

这些文件是独立的,与 css、javaacript 等其他媒体静态文件没有任何关系。

鉴于我需要进行身份验证的静态文件将相当大,我想知道为它们提供服务的最有效方式是什么(顺便说一句,我正在使用 wsgi)。

目前我有这个:

def return_large_file(request, p_filename):
    """                                                                         
    Send a file through Django without loading the whole file into              
    memory at once. The FileWrapper will turn the file object into an           
    iterator for chunks of 8KB.                                                 
    """
    if not os.path.exists(p_filename):
        raise Exception('File %s does not exist!')

    try:
        _content_type = mimetypes.guess_type(p_filename)
    except:
        _content_type = 'application/octet-stream'

    wrapper = FileWrapper(file(p_filename))
    response = HttpResponse(wrapper, content_type=_content_type)
    response['Content-Length'] = os.path.getsize(p_filename)
    return response
4

1 回答 1

1

我目前正在使用类似于您上面使用的功能。

我在想一旦性能/效率成为问题,我会使用 Apache mod-auth-external对给定文件进行自定义授权。

请注意,我提供这个答案不是基于我的经验,而是我自己的研究引导我。

于 2011-07-13T21:06:02.593 回答