我第一次在 Heroku 上成功地部署了一个实时的 Django 应用程序。但是,我没有意识到 Heroku 不存储媒体文件——我希望管理员能够将图像上传到应用程序——所以我正在尝试设置 Amazon S3。这是我第一次做这个设置。
1)我的 settings.py 文件包括这个......
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': "os.path.join(BASE_DIR, 'db.sqlite3')", # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': '',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
MEDIA_ROOT = '/media/'
MEDIA_URL = S3_URL + MEDIA_ROOT
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'listing.urls'
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'listing.wsgi.application'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
'noticeboard',
'django_extensions',
'south',
'PIL',
'storages',
'boto',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
DATABASES['default'] = dj_database_url.config()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
ASW_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
S3_BUCKET_NAME = os.environ['S3_BUCKET_NAME']
STATICFILES_STORAGE = 'storages.backends.s3boto.s3BotoStorage'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.s3BotoStorage'
S3_URL = 'http://s3.amazonaws.com/%s' % S3_BUCKET_NAME
2)我已经更新了我的 requirements.txt 文件以包含boto
和storages
模块
3) 我已经设置了我的 Herokuconfig
文件以包含我的存储桶名称、AWS 密钥和密钥。
这是我从管理员上传图片时遇到的错误,似乎它仍在尝试访问 Heroku 而不是 S3 上的文件:
Exception Type: OSError
Exception Value:
[Errno 30] Read-only file system: '/Users'
Exception Location: /app/.heroku/python/lib/python2.7/os.py in makedirs, line 157
Python Executable: /app/.heroku/python/bin/python
Python Version: 2.7.4
我已经阅读了一些关于此的博客文章,它们说了同样的话,但我不知道为什么这不起作用!