这种在 Django 中发送 couchdb 附件的方法对于一般用途来说是否足够高效?

发布于 2024-09-27 21:31:59 字数 653 浏览 0 评论 0原文

目前,我正在使用以下代码允许用户下载附加到 couchdb 文档的文件。我正在将 couchdbkit 与 Django 一起使用。

def get_file(request, user_id):
    user = User.objects.get(pk = user_id)
    application = user.get_profile().application()
    attachment_name = request.GET.get('name', None)
    assert attachment_name

    attachment = application.fetch_attachment(attachment_name, stream=False)
    return HttpResponse(attachment, content_type=application._attachments[attachment_name]['content_type'])

这可行,但我担心机器上的内存使用情况。这种方法是否高效,或者大文件会在传递给 HttpResponse 之前转储到内存中?我已经使用了stream=True,但我不确定如何最好地测试它。至少可以说,couchdbkit 上的文档很少。我打算在整个应用程序中使用类似的东西,并希望第一次就能正确使用该方法。 :)

Currently I'm using the below code to allow a user to download a file that's attached to a couchdb document. I'm using couchdbkit with Django.

def get_file(request, user_id):
    user = User.objects.get(pk = user_id)
    application = user.get_profile().application()
    attachment_name = request.GET.get('name', None)
    assert attachment_name

    attachment = application.fetch_attachment(attachment_name, stream=False)
    return HttpResponse(attachment, content_type=application._attachments[attachment_name]['content_type'])

This works, but I am concerned about memory usage on the machine. Is this method efficient, or will large files dump to memory before being passed to the HttpResponse? I have used stream=True, but I'm not sure best how to test this. The documentation on couchdbkit is sparse to say the least. I intend to use something similar to this throughout the application and want to get the method right the first time. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文