当我尝试使用 AWS S3BotoStorage 时出现 "The SECRET_KEY setting must not be empty" 错误
Getting a "The SECRET_KEY setting must not be empty" error when I try to use AWS S3BotoStorage
我正在尝试按照本教程使用 AWS S3 存储桶存储我的媒体和静态文件:https://www.caktusgroup.com/blog/2014/11/10/Using-Amazon-S3-to-store-your-Django-sites-static-and-media-files/
在提及我的问题之前,我要指出,我认为这个问题是由于 "circulatory import" 问题引起的,如此处讨论:Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty 但我似乎无法弄清楚如何修复它。
这是我的 settings.py
:
import os
from ebdjango.custom_storages import *
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'secretkey%4!'
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'ebdjangoapp',
'storages',
)
AWS_HEADERS = { # see http://developer.yahoo.com/performance/rules.html#expires
'Expires': 'Thu, 31 Dec 2099 20:00:00 GMT',
'Cache-Control': 'max-age=94608000',
}
AWS_STORAGE_BUCKET_NAME = 'bucketname'
AWS_ACCESS_KEY_ID = 'SECRETKEYID'
AWS_SECRET_ACCESS_KEY = 'SECRETACCESSKEY'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
STATICFILES_LOCATION = 'static'
STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'custom_storages.StaticStorage'
MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'ebdjango.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'ebdjango.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
我认为问题是因为这些行:
from ebdjango.custom_storages import *
STATICFILES_STORAGE = 'custom_storages.StaticStorage'
DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'
ebdjango.custome_storages
是这样的:
from django.conf import settings
from storages.backends.s3boto import S3BotoStorage
class StaticStorage(S3BotoStorage):
location = settings.STATICFILES_LOCATION
class MediaStorage(S3BotoStorage):
location = settings.MEDIAFILES_LOCATION
这是我遇到的错误:
File "/ebdjango/ebdjango/settings.py", line 15, in <module>
from ebdjango.custom_storages import *
File "/ebdjango/ebdjango/custom_storages.py", line 3, in <module>
from storages.backends.s3boto import S3BotoStorage
File "/.virtualenvs/CMS/lib/python3.4/site-packages/storages/backends/s3boto.py", line 69, in <module>
class S3BotoStorageFile(File):
File "/.virtualenvs/CMS/lib/python3.4/site-packages/storages/backends/s3boto.py", line 89, in S3BotoStorageFile
buffer_size = setting('AWS_S3_FILE_BUFFER_SIZE', 5242880)
File "/.virtualenvs/CMS/lib/python3.4/site-packages/storages/utils.py", line 22, in setting
return getattr(settings, name, default)
File "/.virtualenvs/CMS/lib/python3.4/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/.virtualenvs/CMS/lib/python3.4/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/.virtualenvs/CMS/lib/python3.4/site-packages/django/conf/__init__.py", line 113, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
知道如何解决这个问题吗?
正如@frakelinsijo 所说,将 SECRET_KEY
移动到 import
语句之上是有效的。
然而,我使用的解决方案不是导入 from ebdjango.custom_storages import *
然后将其引用为 STATICFILES_STORAGE = 'custom_storages.StaticStorage'
,我决定根本没有导入语句并执行 STATICFILES_STORAGE = 'ebdjangoapp.custom_storages.StaticStorage'
。
我正在尝试按照本教程使用 AWS S3 存储桶存储我的媒体和静态文件:https://www.caktusgroup.com/blog/2014/11/10/Using-Amazon-S3-to-store-your-Django-sites-static-and-media-files/
在提及我的问题之前,我要指出,我认为这个问题是由于 "circulatory import" 问题引起的,如此处讨论:Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty 但我似乎无法弄清楚如何修复它。
这是我的 settings.py
:
import os
from ebdjango.custom_storages import *
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'secretkey%4!'
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'ebdjangoapp',
'storages',
)
AWS_HEADERS = { # see http://developer.yahoo.com/performance/rules.html#expires
'Expires': 'Thu, 31 Dec 2099 20:00:00 GMT',
'Cache-Control': 'max-age=94608000',
}
AWS_STORAGE_BUCKET_NAME = 'bucketname'
AWS_ACCESS_KEY_ID = 'SECRETKEYID'
AWS_SECRET_ACCESS_KEY = 'SECRETACCESSKEY'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
STATICFILES_LOCATION = 'static'
STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'custom_storages.StaticStorage'
MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'ebdjango.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'ebdjango.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
我认为问题是因为这些行:
from ebdjango.custom_storages import *
STATICFILES_STORAGE = 'custom_storages.StaticStorage'
DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'
ebdjango.custome_storages
是这样的:
from django.conf import settings
from storages.backends.s3boto import S3BotoStorage
class StaticStorage(S3BotoStorage):
location = settings.STATICFILES_LOCATION
class MediaStorage(S3BotoStorage):
location = settings.MEDIAFILES_LOCATION
这是我遇到的错误:
File "/ebdjango/ebdjango/settings.py", line 15, in <module>
from ebdjango.custom_storages import *
File "/ebdjango/ebdjango/custom_storages.py", line 3, in <module>
from storages.backends.s3boto import S3BotoStorage
File "/.virtualenvs/CMS/lib/python3.4/site-packages/storages/backends/s3boto.py", line 69, in <module>
class S3BotoStorageFile(File):
File "/.virtualenvs/CMS/lib/python3.4/site-packages/storages/backends/s3boto.py", line 89, in S3BotoStorageFile
buffer_size = setting('AWS_S3_FILE_BUFFER_SIZE', 5242880)
File "/.virtualenvs/CMS/lib/python3.4/site-packages/storages/utils.py", line 22, in setting
return getattr(settings, name, default)
File "/.virtualenvs/CMS/lib/python3.4/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/.virtualenvs/CMS/lib/python3.4/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/.virtualenvs/CMS/lib/python3.4/site-packages/django/conf/__init__.py", line 113, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
知道如何解决这个问题吗?
正如@frakelinsijo 所说,将 SECRET_KEY
移动到 import
语句之上是有效的。
然而,我使用的解决方案不是导入 from ebdjango.custom_storages import *
然后将其引用为 STATICFILES_STORAGE = 'custom_storages.StaticStorage'
,我决定根本没有导入语句并执行 STATICFILES_STORAGE = 'ebdjangoapp.custom_storages.StaticStorage'
。