Configuring django-compressor with remote storage

2019-04-28 16:49发布

问题:

My scenario

I'm using django-storage to have the files served via Amazon S3. This means that when I do ./manage.py collectstatic, the files will be saved on my bucket at amazon and not on the local file system.

To compress the files I do: "./manage.py compress" Which gives this error:

Error: An error occured during rendering: [Errno 2] No such file or directory: u'/home/user/project/static/less/bootstrap.less'

Since the file isn't on my local filesystem.

"Due to the way Django Compressor processes files, it requires the files to be processed (in the {% compress %} block) to be available in a local file system cache." http://django_compressor.readthedocs.org/en/latest/remote-storages/

Question

How do I make django-compress work with django-storage (amazon s3)?

What I've tried to do so far

Make collectstatic save files both local and on S3. Since it's mentioned mentioned in the documentation at the django-compressor page, there should be some good way to do it. How?

Configuration

STATIC_URL = 'http://mybucket.s3-website-eu-west-1.amazonaws.com/'
STATIC_ROOT = os.path.join(PROJECT_DIR,"static/")
STATICFILES_FINDERS = (
     'django.contrib.staticfiles.finders.FileSystemFinder',
     'django.contrib.staticfiles.finders.AppDirectoriesFinder',
     'compressor.finders.CompressorFinder',
)
STATICFILES_STORAGE = DEFAULT_FILE_STORAGE = 'index.storage.CachedS3BotoStorage' #defined as it is in the documentation

AWS_ACCESS_KEY_ID = "xxx"
AWS_SECRET_ACCESS_KEY = "xxx"
AWS_STORAGE_BUCKET_NAME = "xxxx"

COMPRESS_URL = STATIC_URL
COMPRESS_OFFLINE = True

COMPRESS_PRECOMPILERS = (
    ('text/less', 'lessc {infile} {outfile}'),
)

回答1:

I think the only setting you are missing is COMPRESS_ROOT.

I have django-compressor working very nicely with S3. Here's my configuration:

DEFAULT_FILE_STORAGE = 'g2k_utils.s3storage.S3BotoStorage'
STATICFILES_STORAGE = DEFAULT_FILE_STORAGE
COMPRESS_ROOT = '/home/user/website/static/' # Where my SCSS, JS files are stored
COMPRESS_STORAGE = DEFAULT_FILE_STORAGE 
COMPRESS_OFFLINE = True