2016-11-27 76 views
1

我有問題找到問題的問題。Django-Compressor使用amazonaws和heroku引發UncompressableFileError與django-storages

我有設置文件夾,裏面我有local.py,common.py和production.py。所有工作都很好,它在localhost上壓縮,但不在heroku上。

當我部署我得到錯誤:

Internal Server Error:/

UncompressableFileError at/
'https://xxxxx.s3.amazonaws.com/static/css/stylesheet.css' isn't accessible via COMPRESS_URL ('//xxxxx.s3.amazonaws.com/static/') and can't be compressed 

common.py

# STATIC FILE CONFIGURATION 
# ------------------------------------------------------------------------------ 
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root 
STATIC_ROOT = str(ROOT_DIR('staticfiles')) 

# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url 
STATIC_URL = '/static/' 

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS 
STATICFILES_DIRS = (
    str(APPS_DIR.path('static')), 
) 

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders 
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
    'compressor.finders.CompressorFinder', 
) 

# MEDIA CONFIGURATION 
# ------------------------------------------------------------------------------ 
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root 
MEDIA_ROOT = str(APPS_DIR('media')) 

# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url 
MEDIA_URL = '/media/' 


COMPRESS_OFFLINE_CONTEXT = { 
    'STATIC_URL': STATIC_URL, 
    'MEDIA_URL': MEDIA_URL, 
} 

COMPRESS_ENABLED=True 

production.py

DEFAULT_FILE_STORAGE = 'config.custom_storages.MediaStorage' 
THUMBNAIL_DEFAULT_STORAGE = DEFAULT_FILE_STORAGE 


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_S3_CUSTOM_DOMAIN = '{}.s3.amazonaws.com'.format(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. 
MEDIAFILES_LOCATION = 'media' 
MEDIA_URL = "//%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION) 


# Static Assests 
# ------------------------ 
COMPRESS_ROOT = STATIC_ROOT 
STATICFILES_STORAGE = 'config.custom_storages.CachedS3BotoStaticStorage' 
COMPRESS_STORAGE = 'config.custom_storages.CachedS3BotoStaticStorage' 

AWS_S3_SECURE_URLS = True 

STATICFILES_LOCATION = 'static' 
STATIC_URL = "//%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION) 


# See: https://github.com/antonagestam/collectfast 
# For Django 1.7+, 'collectfast' should come before 
# 'django.contrib.staticfiles' 
AWS_PRELOAD_METADATA = True 
INSTALLED_APPS = ['collectfast', ] + INSTALLED_APPS 

custom_storages.py

from django.conf import settings 
from storages.backends.s3boto import S3BotoStorage 
from django.core.files.storage import get_storage_class 


class StaticStorage(S3BotoStorage): 
    location = settings.STATICFILES_LOCATION 
    file_overwrite = True 


class MediaStorage(S3BotoStorage): 
    location = settings.MEDIAFILES_LOCATION 
    file_overwrite = False 


class CachedS3BotoStaticStorage(S3BotoStorage): 
    """ 
    S3 storage backend that saves the files locally, too. 
    """ 
    location = 'static' 

    def __init__(self, *args, **kwargs): 
     super(CachedS3BotoStaticStorage, self).__init__(*args, **kwargs) 
     self.local_storage = get_storage_class(
      "compressor.storage.CompressorFileStorage")() 

    def save(self, name, content): 
     name = super(CachedS3BotoStaticStorage, self).save(name, content) 
     self.local_storage._save(name, content) 
     return name 

回答

1

我花了整個晚上試圖解決這一點,我做了這個代碼變化不大:

isn't accessible via COMPRESS_URL ('//xxxxx.s3.amazonaws.com/static/') 

我認爲這可以增加HTTP或HTTPS和我有

AWS_S3_SECURE_URLS = True 

所以只是測試我加硬編碼

https: 

這樣

STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION) 

它開始工作,它很棒。但任何人都可以解釋爲什麼它不認可或添加HTTP或HTTPS?