在Heroku + Amazon S3上设置Django应用程序

时间:2013-07-28 18:36:42

标签: django heroku amazon-s3

我第一次在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文件以包含botostorages模块

3)我已设置我的Heroku config文件以包含我的存储桶名称,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

我已经阅读了一些关于此事的博客文章说同样的事情,但我不知道为什么这不起作用!

0 个答案:

没有答案