简体   繁体   中英

Django 1.8 Cache busting + Amazon S3

I´ve been doing some research, and I have found articles explaining how to use Django's(1.8) cache busting, but they don´t explain my situation.

I am using S3 (and it works) with the following setting in settings.py:

STATICFILES_STORAGE= 'pms.s3utils.StaticRootS3BotoStorage'

In order to use cache busting the docs say I have to set: STATICFILES_STORAGE='django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

I don´t know what setting to use in order to use both S3 and cache busting.

Thanks!

So I finally found a workaround.

In order to be able to upload my files to 2 different folders (static and uploads) in my S3 bucket I have this in my settings.py file:

STATICFILES_STORAGE = 'myapp.s3utils.StaticRootS3BotoStorage'
DEFAULT_FILE_STORAGE = 'myapp.s3utils.MediaRootS3BotoStorage'

And this in the myapp.s3utils.py file:

from storages.backends.s3boto import S3BotoStorage

StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static')
MediaRootS3BotoStorage  = lambda: S3BotoStorage(location='uploads')

But I couldn´t use Django´s cache busting. The solution was to change my myapp.s3utils.py file to:

from storages.backends.s3boto import S3BotoStorage

from django.contrib.staticfiles.storage import ManifestFilesMixin

class CustomS3Storage(ManifestFilesMixin, S3BotoStorage):
    pass

StaticRootS3BotoStorage = lambda: CustomS3Storage(location='static')
MediaRootS3BotoStorage  = lambda: S3BotoStorage(location='uploads')

Basically it adds the ManiFestfilesMixin , which allows you to use cache busting.

As a side note, you can see that I am only using cache busting for the static files, but not for the uploads files. That is why the MediaRootS3BotoStorage calls the S3BotoStorage class instead of the CustomS3Storage . I do it this way because the uploads files are not stored in my server (the static files are), they are stored directly in the S3 bucket, so when I ran the collectstatic they are not on my server, so I don´t have to add the hash to the names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM