2016-08-25 68 views
0

我无法将我的模型中的ImageField保存为s3。我可以将我的静态文件保存为s3而不会出现任何问题。我是cookie cutter django的最新版本。我已经部署到heroku并将我的环境变量存储在那里,我已经仔细检查了我的AWS设置,并可以在我的s3存储桶中看到静态文件。使用cookie-cutter-django将存储介质存储到S3问题

production.py

from __future__ import absolute_import, unicode_literals 

from boto.s3.connection import OrdinaryCallingFormat 
from django.utils import six 


from .common import * # noqa 

# SECRET CONFIGURATION 
# ------------------------------------------------------------------------------ 
# See: https://docs.djangoproject.com/en/dev/ref/settings/#secret-key 
# Raises ImproperlyConfigured exception if DJANGO_SECRET_KEY not in os.environ 
SECRET_KEY = env('DJANGO_SECRET_KEY') 


# This ensures that Django will be able to detect a secure connection 
# properly on Heroku. 
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') 
# Use Whitenoise to serve static files 
# See: https://whitenoise.readthedocs.io/ 
WHITENOISE_MIDDLEWARE = ('whitenoise.middleware.WhiteNoiseMiddleware',) 
MIDDLEWARE_CLASSES = WHITENOISE_MIDDLEWARE + MIDDLEWARE_CLASSES 


# SECURITY CONFIGURATION 
# ------------------------------------------------------------------------------ 
# See https://docs.djangoproject.com/en/1.9/ref/middleware/#module-django.middleware.security 
# and https://docs.djangoproject.com/ja/1.9/howto/deployment/checklist/#run-manage-py-check-deploy 

# set this to 60 seconds and then to 518400 when you can prove it works 
SECURE_HSTS_SECONDS = 60 
SECURE_HSTS_INCLUDE_SUBDOMAINS = env.bool(
    'DJANGO_SECURE_HSTS_INCLUDE_SUBDOMAINS', default=True) 
SECURE_CONTENT_TYPE_NOSNIFF = env.bool(
    'DJANGO_SECURE_CONTENT_TYPE_NOSNIFF', default=True) 
SECURE_BROWSER_XSS_FILTER = True 
SESSION_COOKIE_SECURE = True 
SESSION_COOKIE_HTTPONLY = True 
SECURE_SSL_REDIRECT = env.bool('DJANGO_SECURE_SSL_REDIRECT', default=True) 
CSRF_COOKIE_SECURE = True 
CSRF_COOKIE_HTTPONLY = True 
X_FRAME_OPTIONS = 'DENY' 

# SITE CONFIGURATION 
# ------------------------------------------------------------------------------ 
# Hosts/domain names that are valid for this site 
# See https://docs.djangoproject.com/en/1.6/ref/settings/#allowed-hosts 
ALLOWED_HOSTS = env.list('DJANGO_ALLOWED_HOSTS', default=['website.org']) 
# END SITE CONFIGURATION 

INSTALLED_APPS += ('gunicorn',) 


# STORAGE CONFIGURATION 
# ------------------------------------------------------------------------------ 
# Uploaded Media Files 
# ------------------------ 
# See: http://django-storages.readthedocs.io/en/latest/index.html 
INSTALLED_APPS += (
    'storages', 
) 

AWS_ACCESS_KEY_ID = env('DJANGO_AWS_ACCESS_KEY_ID') 
AWS_SECRET_ACCESS_KEY = env('DJANGO_AWS_SECRET_ACCESS_KEY') 
AWS_STORAGE_BUCKET_NAME = env('DJANGO_AWS_STORAGE_BUCKET_NAME') 
AWS_AUTO_CREATE_BUCKET = True 
AWS_QUERYSTRING_AUTH = False 
AWS_S3_CALLING_FORMAT = OrdinaryCallingFormat() 

# AWS cache settings, don't change unless you know what you're doing: 
AWS_EXPIRY = 60 * 60 * 24 * 7 

# TODO See: https://github.com/jschneier/django-storages/issues/47 
# Revert the following and use str after the above-mentioned bug is fixed in 
# either django-storage-redux or boto 
AWS_HEADERS = { 
    'Cache-Control': six.b('max-age=%d, s-maxage=%d, must-revalidate' % (
     AWS_EXPIRY, AWS_EXPIRY)) 
} 

# URL that handles the media served from MEDIA_ROOT, used for managing 
# stored files. 
MEDIA_URL = 'https://s3.amazonaws.com/%s/' % AWS_STORAGE_BUCKET_NAME 


# Static Assets 
# ------------------------ 
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' 

models.py

from django.db import models 

class BaseModel(models.Model): 
    created_date = models.DateTimeField(auto_now_add=True) 
    modified_date = models.DateTimeField(auto_now=True) 

    class Meta: 
     abstract = True 


class Project(BaseModel): 
    name = models.CharField(max_length=100) 
    description = models.CharField(max_length=2000) 
    body = models.TextField(max_length=10000) 
    slug = models.SlugField(max_length=75, db_index=True) 
    start_date = models.DateField() 
    location_name = models.CharField(max_length=100, blank=True) 
    address1 = models.CharField(max_length=100, blank=True) 
    address2 = models.CharField(max_length=100, blank=True) 
    city = models.CharField(max_length=100, blank=True) 
    state = models.CharField(max_length=100, blank=True) 
    country = models.CharField(max_length=100, blank=True) 
    main_image = models.ImageField(upload_to='projects/project_images') 
    flyer = models.ImageField(upload_to='projects/project_images', blank=True) 
    image1 = models.ImageField(upload_to='projects/project_images', blank=True) 
    image2 = models.ImageField(upload_to='projects/project_images', blank=True) 
    image3 = models.ImageField(upload_to='projects/project_images', blank=True) 
    image4 = models.ImageField(upload_to='projects/project_images', blank=True) 
    image5 = models.ImageField(upload_to='projects/project_images', blank=True) 
    image6 = models.ImageField(upload_to='projects/project_images', blank=True) 
    image7 = models.ImageField(upload_to='projects/project_images', blank=True) 
    image8 = models.ImageField(upload_to='projects/project_images', blank=True) 

    class Meta: 
     ordering = ["-start_date"] 
     index_together = (('id', 'slug'),) 

    def __str__(self): # __unicode__ on Python 2 
     return self.name 

回答

0

道歉,我曾在以前的承诺,并添加需要的:

# See:http://stackoverflow.com/questions/10390244/ 
from storages.backends.s3boto import S3BotoStorage 
StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static') 
MediaRootS3BotoStorage = lambda: S3BotoStorage(location='media') 
DEFAULT_FILE_STORAGE = 'config.settings.production.MediaRootS3BotoStorage' 

MEDIA_URL = 'https://s3.amazonaws.com/%s/media/' % AWS_STORAGE_BUCKET_NAME 

# Static Assets 
# ------------------------ 

STATIC_URL = 'https://s3.amazonaws.com/%s/static/' % AWS_STORAGE_BUCKET_NAME 
STATICFILES_STORAGE = 'config.settings.production.StaticRootS3BotoStorage' 
# See: https://github.com/antonagestam/collectfast 
# For Django 1.7+, 'collectfast' should come before 
# 'django.contrib.staticfiles' 
AWS_PRELOAD_METADATA = True