Django 1.11 + 用于 collectstatic 的 Amazon S3
Django 1.11 + Amazon S3 for collectstatic
我刚刚尝试在 eu-central-1
上使用 Amazon S3 在生产环境中提供我的静态文件。我正在使用 Elastic Beanstalk 和 Django 1.11。此外,我为它使用 boto3 和包 Django Storages 。我的问题是,即使 collectstatic 有效并且文件现在位于 S3 存储桶中,Django 管理员仍然不使用静态文件。
关于上下文,让我给你我使用的设置:
import os
from django.core.exceptions import ImproperlyConfigured
# Static files (CSS, JavaScript, Images)
STATICFILES_DIRS = [BASE_DIR.parent / 'myproject' / 'static']
INSTALLED_APPS += ['storages', ]
def get_env_variable(var_name):
"""Get the environment variable or return exception."""
try:
return os.environ[var_name]
except KeyError:
error_msg = 'Set the {} environment variable'.format(var_name)
raise ImproperlyConfigured(error_msg)
AWS_ACCESS_KEY_ID = get_env_variable("ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = get_env_variable("SECRET_ACCESS_KEY")
AWS_S3_SIGNATURE_VERSION = 's3v4'
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
AWS_STORAGE_BUCKET_NAME = get_env_variable("BUCKET_NAME")
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
AWS_LOCATION = 'static'
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATICFILES_STORAGE = 'config.settings.aws.utils.StaticRootS3BotoStorage'
DEFAULT_FILE_STORAGE = 'config.settings.aws.utils.MediaRootS3BotoStorage'
MEDIA_URL = 'https://%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = MEDIA_URL
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
S3_USE_SIGV4 = True
这些是设置,这是我写的utils函数:
from storages.backends.s3boto3 import S3Boto3Storage
def StaticRootS3BotoStorage(): return S3Boto3Storage(location='static')
def MediaRootS3BotoStorage(): return S3Boto3Storage(location='media')
有人知道哪里出了问题吗?为什么 collectstatic 工作,但 Django Admin 不应用 css 和 javascript?
我只是有一个'/'太多了。
我刚刚更正了以下行:
AWS_S3_CUSTOM_DOMAIN = '%s.s3.eu-central-1.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
现在可以使用了。
我刚刚尝试在 eu-central-1
上使用 Amazon S3 在生产环境中提供我的静态文件。我正在使用 Elastic Beanstalk 和 Django 1.11。此外,我为它使用 boto3 和包 Django Storages 。我的问题是,即使 collectstatic 有效并且文件现在位于 S3 存储桶中,Django 管理员仍然不使用静态文件。
关于上下文,让我给你我使用的设置:
import os
from django.core.exceptions import ImproperlyConfigured
# Static files (CSS, JavaScript, Images)
STATICFILES_DIRS = [BASE_DIR.parent / 'myproject' / 'static']
INSTALLED_APPS += ['storages', ]
def get_env_variable(var_name):
"""Get the environment variable or return exception."""
try:
return os.environ[var_name]
except KeyError:
error_msg = 'Set the {} environment variable'.format(var_name)
raise ImproperlyConfigured(error_msg)
AWS_ACCESS_KEY_ID = get_env_variable("ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = get_env_variable("SECRET_ACCESS_KEY")
AWS_S3_SIGNATURE_VERSION = 's3v4'
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
AWS_STORAGE_BUCKET_NAME = get_env_variable("BUCKET_NAME")
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
AWS_LOCATION = 'static'
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATICFILES_STORAGE = 'config.settings.aws.utils.StaticRootS3BotoStorage'
DEFAULT_FILE_STORAGE = 'config.settings.aws.utils.MediaRootS3BotoStorage'
MEDIA_URL = 'https://%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = MEDIA_URL
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
S3_USE_SIGV4 = True
这些是设置,这是我写的utils函数:
from storages.backends.s3boto3 import S3Boto3Storage
def StaticRootS3BotoStorage(): return S3Boto3Storage(location='static')
def MediaRootS3BotoStorage(): return S3Boto3Storage(location='media')
有人知道哪里出了问题吗?为什么 collectstatic 工作,但 Django Admin 不应用 css 和 javascript?
我只是有一个'/'太多了。
我刚刚更正了以下行:
AWS_S3_CUSTOM_DOMAIN = '%s.s3.eu-central-1.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
现在可以使用了。