Error when trying to compress static files when us

2019-07-19 14:18发布

I have setup an AWS S3 bucket in order to transfer my static files in a remote CDN using the application django-storages, everything worked fine until I tried to compress my static files before uploading to S3 using django_compressor.

I have setup all the variables according django_compressor documentation for django-storages (https://django_compressor.readthedocs.org/en/latest/remote-storages/index.html)

I uploaded all the files in S3 using 'manage.py collectstatic' then:

When I do 'manage.py compress' I get this error:

CommandError: An error occured during rendering ../templates/base.html: 'https://my_bucket.s3.amazonaws.com/css/bootstrap.2.3.1.css' isn't accessible via COMPRESS_URL ('https://my_bucket.s3-external-3.amazonaws.com/') and can't be compressed

What's wrong with my setup?

Here is my settings.py configuration for django-storages and django_compressor:

COMPRESS_URL = 'https://mybucket_name.s3-external-3.amazonaws.com/'
STATIC_URL = COMPRESS_URL
DEFAULT_FILE_STORAGE = 'my_project.boto_custom.CachedS3BotoStorage'

AWS_ACCESS_KEY_ID = 'XXX'
AWS_SECRET_ACCESS_KEY = 'XXX'
AWS_STORAGE_BUCKET_NAME = 'mybucket_name'

COMPRESS_ROOT = STATIC_ROOT

COMPRESS_STORAGE = 'my_project.boto_custom.CachedS3BotoStorage'
STATICFILES_STORAGE = 'my_project.boto_custom.CachedS3BotoStorage'

COMPRESS_OFFLINE = True

Thanks for your help

2条回答
劳资没心,怎么记你
2楼-- · 2019-07-19 14:53

If you have separate S3 buckets for static and media you can also put it in your subclass of S3BotoStorage like so:

class CachedS3BotoStorage(S3BotoStorage):
  custom_domain = 'my_bucket.s3-external-3.amazonaws.com'

(or better yet set it to settings.AWS_S3_CUSTOM_STATIC_DOMAIN or something)

查看更多
欢心
3楼-- · 2019-07-19 14:59

I fixed it by adding one variable and it worked:

AWS_S3_CUSTOM_DOMAIN = 'my_bucket.s3-external-3.amazonaws.com'
查看更多
登录 后发表回答