Django custom storage for Thumbor backend.
A Django custom storage for Thumbor backend.
This app provide 2 classes ThumborStorage and ThumborMigrationStorage. The last one is a storage you can use for Imagefield initialy using a FileSystemStorage you want to migrate to Thumbor without batch-moving all of them. That way, Django continues to serve them from the file system until you change the image on that field.
pip install django-thumborstorage
Add django_thumborstorage in your INSTALLED_APPS.
And set the following:
THUMBOR_SERVER = 'http://localhost:8888' THUMBOR_SECURITY_KEY = 'MY_SECURE_KEY' # This may be a different host than THUMBOR_SERVER # only reachable by your Django server. THUMBOR_RW_SERVER = 'http://localhost:8888'
Just set the storage parameter in the ImageField you want to manage with Thumbor:
from django_thumborstorage.storages import ThumborStorage class Stuff(models.Model): def upload_path(instance, filename): return 'stuffs/%s' % filename photo = models.ImageField(upload_to=upload_path, storage=ThumborStorage(), height_field='photo_height', width_field='photo_width') photo_height = models.IntegerField(blank=True, null=True) photo_width = models.IntegerField(blank=True, null=True)
You can get the Thumbor uuid from the <ImageField> instance using:
This is useful to generate_url() with Django-thumbor when original files are stored on Thumbor. Thus, you can pass the key as url parameter.