Django-Compressor使用amazonaws和heroku在django-storages中抛出UncompressableFileError [英] Django-Compressor throws UncompressableFileError with django-storages using amazonaws and heroku

查看:171
本文介绍了Django-Compressor使用amazonaws和heroku在django-storages中抛出UncompressableFileError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题找到问题的问题。



我有设置文件夹,里面我有local.py,common.py和production.py。所有的工作都很好,它在压缩本地主机,但不是在英雄上。



当我部署它,我得到错误:

 内部服务器错误:/ 

/
'中的UncompressableFileError https://xxxxx.s3.amazonaws.com/static/css /stylesheet.css'无法通过COMPRESS_URL('//xxxxx.s3.amazonaws.com/static/')访问,无法压缩

common.py

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

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

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


#请参阅:https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS =(
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compress.finders.CompressorFinder',


#媒体配置
#-------------------------------------- ----------------------------------------
#查看:https:/ /docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = str(APPS_DIR('media'))

#参见: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'
THUMBNA IL_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缓存设置,除非你知道你在做什么,否则不要改变:
AWS_EXPIRY = 60 * 60 * 24 * 7

#TODO请参阅:https:// github.com/jschneier/django-storages/issues/47
#在
#中修复以上内容并使用str,否则django-storage-redux或boto
AWS_HEADERS = {
'Cache-Control':six.b('max-age =%d,s-maxage =%d,must-revalidate'%(
AWS_EXPIRY,AWS_EXPIRY))
}

#处理从M提供的媒体的URL EDIA_ROOT,用于管理
#存储的文件。
MEDIAFILES_LOCATION ='media'
MEDIA_URL =//%s /%s /%(AWS_S3_CUSTOM_DOMAIN,MEDIAFILES_LOCATION)


#静态资产
#------------------------
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)


#参见:https://github.com/antonagestam/collectfast
#对于Django 1.7+,'collectfast'应该在
之前#'django.contrib.staticfiles'
AWS_PRELOAD_METADATA = True
INSTALLED_APPS = ['collectfast',] + INSTALLED_APPS

custom_storages.py

 从django.conf导入设置
从storages.backends。 s3boto从django.core.files.storage导入S3BotoStorage
导入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存储后端也可以在本地保存文件。

location ='static'

def __init __(self,* args,** kwargs):
super(CachedS3BotoStaticStorage,self).__ init __ * args,** kwargs)
self.local_storage = get_storage_class(
compress.storage.CompressorFileStorage)()

def save(self,name,content):
name = super(CachedS3BotoStaticStorage,self).save(name,content)
self.local_storage._save(name,content)
返回名称
/ pre>

解决方案

我花了整整一个晚上尝试解决这个问题,我在这段代码中做了一些改动:

 无法通过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?


I am having problem to locate the issue of the problem.

I have settings folder and inside I have local.py, common.py and production.py. All is working great and it is compressing on localhost, but not on heroku.

When I deploy it I am getting error:

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

解决方案

I spent the whole night trying to solve this and I made a little change in this code:

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

I thought it can add http or https and I have

AWS_S3_SECURE_URLS = True

So just for test I added hardcoded

https:

like this

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

and it started working, it is great. But can anyone explain why it is not recognizing or adding http or https to it?

这篇关于Django-Compressor使用amazonaws和heroku在django-storages中抛出UncompressableFileError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆