在开发和Amazon S3中同时提供静态文件

时间:2017-11-01 04:24:34

标签: amazon-s3 django-staticfiles

我想从Amazon S3和本地服务器安装静态文件?

我也不知道如何设置MEDIA_URL STATIC_ROOTMEDIA_ROOT

上下文:

我使用Amazon S3django-boto提供我的静态文件,我的settings/base.py是:

STATICFILES_LOCATION = 'assets'
STATICFILES_STORAGE = 'custom_storages.StaticStorage'

STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)

MEDIAFILES_LOCATION = 'media'

MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)

DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'

我的custom_storages.py文件内容为:

from django.conf import settings
# from storages.backends.s3boto3 import S3Boto3Storage
from storages.backends.s3boto import S3BotoStorage


class StaticStorage(S3BotoStorage):
    location = settings.STATICFILES_LOCATION


class MediaStorage(S3BotoStorage):
    location = settings.MEDIAFILES_LOCATION

所有这一切都很好。 当我执行collectstatic时,我的静态文件将被上传到我在Amazon S3中的存储桶。

我遇到的问题是,每次我在cssjs文件中进行更改时,我都需要执行collectstatic命令。

如何设置我的项目(设置)以便一起在本地服务器中提供来自S3和我的django的静态文件?

我有一个settings/development.py文件,其中我将覆盖以下设置:

STATIC_URL = '/assets/'
STATICFILES_LOCATION = 'assets'

MEDIAFILES_LOCATION = 'media/'
MEDIA_URL = MEDIAFILES_LOCATION

STATIC_ROOT = os.path.join(BASE_DIR, "assets")
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

我的urls.py主文件我有这个条件:

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

0 个答案:

没有答案