本地文件系统作为Django中的远程存储 [英] Local filesystem as a remote storage in Django

查看:53
本文介绍了本地文件系统作为Django中的远程存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Amazon S3用作Web服务的一部分.工作流程如下:

I use Amazon S3 as a part of my webservice. The workflow is the following:

  • 用户将大量文件上传到Web服务器.Web服务器首先将它们存储在本地,然后异步上传到S3
  • 用户发送http请求以启动作业(这是对这些上传文件的处理)
  • Web服务要求工作人员完成工作
  • 工人完成工作并将结果上传到S3
  • 用户从Web服务器请求下载链接,并返回 somedbrecord.result_file.url
  • 使用此链接的用户下载结果

要使用文件,我使用 QueuedStorage 后端.我这样启动我的 FileFields :

To work with files I use QueuedStorage backend. I initiate my FileFields like this:

    user_uploaded_file = models.FileField(..., storage=queued_s3storage, ...)
    result_file = models.FileField(..., storage=queued_s3storage, ...)

其中 queued_s3storage 是从 ... backends.QueuedStorage remote 字段设置为'的类的对象...backends.s3boto.S3BotoStorage'.

Where queued_s3storage is an object of class derived from ...backends.QueuedStorage and remote field is set to '...backends.s3boto.S3BotoStorage'.

现在,我计划将整个系统部署在一台计算机上以在本地运行所有内容,我想用基于我本地文件系统的某些内容替换此'... backends.s3boto.S3BotoStorage'

Now I'm planning to deploy the whole system on one machine to run everything locally, I want to replace this '...backends.s3boto.S3BotoStorage' with something based on my local filesystem.

第一个解决方法是使用FakeS3,它可以在本地模拟" S3.可行,但这并不理想,只是额外的不必要开销.

The first workaround was to use FakeS3 which can "emulate" S3 locally. Works, but this is not ideal, just extra unnecessary overhead.

我让Nginx服务器运行并提供特定目录中的静态文件.如何创建我的远程存储"类,该类实际上将文件存储在本地,但是提供了导致Nginx服务的文件的下载链接?(类似于 http://myip:80/filedir/file1 ).django中有针对该类的标准库类吗?

I have Nginx server running and serving static files from particular directories. How do I create my "remote storage" class that actually stores files locally, but provides download links which lead to files served by Nginx? (something like http://myip:80/filedir/file1). Is there a standard library class for that in django?

推荐答案

媒体文件的默认存储后端是本地存储.

您的 settings.py 定义了以下两个环境变量:

Your settings.py defines these two environment variables:

  • MEDIA_ROOT (链接到文档)-这是本地文件存储文件夹的绝对路径
  • MEDIA_URL (链接到文档)-这是Web服务器的HTTP路径(例如'/media/''//%s/media'%HOSTNAME
  • MEDIA_ROOT (link to docs) -- this is the absolute path to the local file storage folder
  • MEDIA_URL (link to docs) -- this is the webserver HTTP path (e.g. '/media/' or '//%s/media' % HOSTNAME

默认存储后端使用这些文件来保存媒体文件.来自Django的默认/全局 settings.py :

These are used by the default storage backend to save media files. From Django's default/global settings.py:

# Default file storage mechanism that holds media.
DEFAULT_FILE_STORAGE = 'django.core.files.storage.FileSystemStorage'

此配置的默认存储空间用于 FileFields 中,没有为其提供存储空间 kwarg .也可以这样访问: rom django.core.files.storage import default_storage .

This configured default storage is used in FileFields for which no storage kwarg is provided. It can also be accessed like so: rom django.core.files.storage import default_storage.

因此,如果您要更改存储以供本地开发和生产使用,则可以执行以下操作:

So if you want to vary the storage for local development and production use, you can do something like this:

# file_storages.py
from django.conf import settings
from django.core.files.storage import default_storage
from whatever.backends.s3boto import S3BotoStorage

app_storage = None
if settings.DEBUG == True:
    app_storage = default_storage
else:
    app_storage = S3BotoStorage()

在您的模型中:

# models.py
from file_storages import app_storage

# ...
    result_file = models.FileField(..., storage=app_storage, ...)


最后,您希望nginx直接从 MEDIA_URL 提供文件.只需确保nginx URL与 MEDIA_URL 中的路径匹配即可.


Lastly, you want nginx to serve the files directly from your MEDIA_URL. Just make sure that the nginx URL matches the path in MEDIA_URL.

这篇关于本地文件系统作为Django中的远程存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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