Remote Storages¶
In some cases it’s useful to use a CDN for serving static files such as
those generated by Django Compressor. 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.
Django Compressor provides hooks to automatically have compressed files
pushed to a remote storage backend. Simply set the storage backend
that saves the result to a remote service (see
COMPRESS_STORAGE
).
django-storages¶
So assuming your CDN is Amazon S3, you can use the boto3 storage backend from the 3rd party app django-storages. Some required settings are:
AWS_ACCESS_KEY_ID = 'XXXXXXXXXXXXXXXXXXXXX'
AWS_SECRET_ACCESS_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
AWS_STORAGE_BUCKET_NAME = 'compressor-test'
Next, you need to specify the new CDN base URL and update the URLs to the files in your templates which you want to compress:
COMPRESS_URL = "http://compressor-test.s3.amazonaws.com/"
Note
For staticfiles just set STATIC_URL = COMPRESS_URL
The storage backend to save the compressed files needs to be changed, too:
COMPRESS_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
Using staticfiles¶
If you are using Django’s staticfiles contrib app, you’ll need to use a temporary filesystem cache for Django Compressor to know which files to compress. Since staticfiles provides a management command to collect static files from various locations which uses a storage backend, this is where both apps can be integrated.
Make sure the
COMPRESS_ROOT
and STATIC_ROOT settings are equal since both apps need to look at the same directories when doing their job.You need to create a subclass of the remote storage backend you want to use; below is an example of the boto3 S3 storage backend from django-storages:
from django.core.files.storage import storages from storages.backends.s3boto3 import S3Boto3Storage class CachedS3Boto3Storage(S3Boto3Storage): """ S3 storage backend that saves the files locally, too. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.local_storage = storages.create_storage({ "BACKEND": "compressor.storage.CompressorFileStorage" }) def save(self, name, content): self.local_storage.save(name, content) super().save(name, self.local_storage._open(name)) return name
Set your
COMPRESS_STORAGE
, STATICFILES_STORAGE andCOMPRESS_OFFLINE_MANIFEST_STORAGE
settings to the dotted path of your custom cached storage backend, e.g.'mysite.storage.CachedS3Boto3Storage'
.To have Django correctly render the URLs to your static files, set the STATIC_URL setting to the same value as
COMPRESS_URL
(e.g."http://compressor-test.s3.amazonaws.com/"
).
In the end it might look like this:
STATIC_ROOT = '/path/to/staticfiles'
COMPRESS_ROOT = STATIC_ROOT
STATICFILES_STORAGE = 'mysite.storage.CachedS3BotoStorage'
COMPRESS_STORAGE = STATICFILES_STORAGE
COMPRESS_OFFLINE_MANIFEST_STORAGE = STATICFILES_STORAGE
STATIC_URL = 'https://compressor-test.s3.amazonaws.com/'
COMPRESS_URL = STATIC_URL