Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I made a few, different changes to settings.py</p> <pre><code>AWS_S3_CUSTOM_DOMAIN = 'XXXXXXX.cloudfront.net' #important: no "http://" AWS_S3_SECURE_URLS = True #default, but must set to false if using an alias on cloudfront COMPRESS_STORAGE = 'example_app.storage.CachedS3BotoStorage' #from the docs (linked below) STATICFILES_STORAGE = 'example_app.storage.CachedS3BotoStorage' </code></pre> <p><a href="http://django_compressor.readthedocs.org/en/latest/remote-storages/">Compressor Docs</a></p> <p>This above solution saved the files locally as well as uploaded them to s3. This let me compress the files offline. If you aren't gzipping, the above ought to work for serving compressed files from CloudFront.</p> <p>Adding gzip adds a wrinkle:</p> <p>settings.py</p> <pre><code>AWS_IS_GZIPPED = True </code></pre> <p>though this resulted in an error whenever a compressible file (css and js according to storages) was being pushed to s3 during collectstatic: </p> <blockquote> <p>AttributeError: 'cStringIO.StringO' object has no attribute 'name'</p> </blockquote> <p>This was due to some bizarre error having to do with the compression of the css/js files that I don't understand. These files I need locally, unzipped, and not on s3, so I could do avoid the problem altogether if I tweak the storage subclass referenced above (and provided in the compressor <a href="http://django_compressor.readthedocs.org/en/latest/remote-storages/">docs</a>). </p> <p>new storage.py</p> <pre><code>from os.path import splitext from django.core.files.storage import get_storage_class from storages.backends.s3boto import S3BotoStorage class StaticToS3Storage(S3BotoStorage): def __init__(self, *args, **kwargs): super(StaticToS3Storage, self).__init__(*args, **kwargs) self.local_storage = get_storage_class('compressor.storage.CompressorFileStorage')() def save(self, name, content): ext = splitext(name)[1] parent_dir = name.split('/')[0] if ext in ['.css', '.js'] and not parent_dir == 'admin': self.local_storage._save(name, content) else: filename = super(StaticToS3Storage, self).save(name, content) return filename </code></pre> <p>This then saved all .css and .js files (excluding the admin files, which I serve uncompressed from CloudFront) while pushing the rest of the files to s3 (and not bothering to save them locally, though could easily add the self.local_storage._save line).</p> <p>But when I run compress, I want my compressed .js and .css files to get pushed to s3 so I create another sublcass for compressor to use:</p> <pre><code>class CachedS3BotoStorage(S3BotoStorage): """ django-compressor uses this class to gzip the compressed files and send them to s3 these files are then saved locally, which ensures that they only create fresh copies when they need to """ def __init__(self, *args, **kwargs): super(CachedS3BotoStorage, self).__init__(*args, **kwargs) self.local_storage = get_storage_class('compressor.storage.CompressorFileStorage')() def save(self, filename, content): filename = super(CachedS3BotoStorage, self).save(filename, content) self.local_storage._save(filename, content) return filename </code></pre> <p>Finally, given these new subclasses, I need to update a few settings:</p> <pre><code>COMPRESS_STORAGE = 'example_app.storage.CachedS3BotoStorage' #from the docs (linked below) STATICFILES_STORAGE = 'example_app.storage.StaticToS3Storage' </code></pre> <p>And that is all I have to say about that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload