UncompressableFileError: "scripts / app.js" is not available through COMPRESS_URL ('http://my-bucket.s3-us-west-2.amazonaws.com/') and cannot be compressed

I am trying to use django-compressor and django-storages-redux along with django and Amazon S3 static files. These are my settings:

STATIC_URL = COMPRESS_URL = 'http://my-bucket.s3-us-west-2.amazonaws.com/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 'compressor.finders.CompressorFinder', ) STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'site-static'), ) COMPRESS_PRECOMPILERS = ( ('text/scss', 'sass --scss --compass {infile} {outfile}'), ) COMPRESS_CSS_FILTERS = [ 'compressor.filters.css_default.CssAbsoluteFilter', 'compressor.filters.cssmin.CSSMinFilter', ] DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage' STATICFILES_STORAGE = COMPRESS_STORAGE = 'myapp.apps.mymodel.storage.CachedS3BotoStorage' COMPRESS_OUTPUT_DIR = 'cache' COMPRESS_ENABLED = False AWS_S3_HOST = "s3-us-west-2.amazonaws.com" AWS_ACCESS_KEY_ID = '---' AWS_SECRET_ACCESS_KEY = '---' AWS_STORAGE_BUCKET_NAME = 'my-bucket' AWS_QUERYSTRING_AUTH = False AWS_S3_CUSTOM_DOMAIN = 'my-bucket.s3-us-west-2.amazonaws.com' 

For static files, I use my own backup server, as indicated here http://django-compressor.readthedocs.org/en/latest/remote-storages/

 from django.core.files.storage import get_storage_class from storages.backends.s3boto import S3BotoStorage class CachedS3BotoStorage(S3BotoStorage): """ S3 storage backend that saves the files locally, too. """ def __init__(self, *args, **kwargs): super(CachedS3BotoStorage, self).__init__(*args, **kwargs) self.local_storage = get_storage_class('compressor.storage.CompressorFileStorage')() def save(self, name, content): name = super(CachedS3BotoStorage, self).save(name, content) self.local_storage._save(name, content) return name 

First I ran python manage.py collectstatic , which worked fine and copied all the files to S3.

Now I have a simple template:

 {% load compress static %} <html><head> {% compress js %} <script src="scripts/app.js"></script> <script src="scripts/controllers/main.js"></script> {% endcompress %} </head><body></body></html> 

Opening this related django view in a browser gives me the following exception:

 'scripts/app.js' isn't accessible via COMPRESS_URL ('http://my-bucket.s3-us-west-2.amazonaws.com/') and can't be compressed 

But the file is and is accessible (via http and https). An exception is thrown here: https://github.com/django-compressor/django-compressor/blob/2.0/compressor/base.py#L82

It seems that get_basename(self, url) ( https://github.com/django-compressor/django-compressor/blob/2.0/compressor/base.py#L72 ) is already getting the relative url here.

Does anyone know how to fix this?

Thanks in advance!

+5
source share
1 answer

It looks like get_basename (self, url) ( https://github.com/django-compressor/django-compressor/blob/2.0/compressor/base.py#L72 ) is already getting the relative url here.

This is problem. Using a static template tag to get an absolute URL should fix this.

 {% load compress static %} <html><head> {% compress js %} <script src="{% static "scripts/app.js" %}"></script> <script src="{% static "scripts/controllers/main.js" %}"></script> {% endcompress %} </head><body></body></html> 

You can also see this answer fooobar.com/questions/262087 / ...

0
source

Source: https://habr.com/ru/post/1240187/


All Articles