Storing images and thumbnails on s3 in django

前端 未结 3 1674
后悔当初
后悔当初 2021-01-30 09:50

I\'m trying to get my images thumbnailed and stored on s3 using django-storages, boto, and sorl-thumbnail. I have it working, but it\'s very slow, even with small images. I don\

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-30 10:09

    After looking at the @shadfc django ticket, I reimplemented the monkeypatch as follows:

    from django.core.files.images import ImageFile
    def _get_image_dimensions(self):
        if not hasattr(self, '_dimensions_cache'):
            if getattr(self.storage, 'IGNORE_IMAGE_DIMENSIONS', False):
                self._dimensions_cache = (0, 0)
            else:
                close = self.closed
                self.open()
                self._dimensions_cache = get_image_dimensions(self, close=close)
        return self._dimensions_cache
    ImageFile._get_image_dimensions = _get_image_dimensions
    

    To use it, just add a IGNORE_IMAGE_DIMENSIONS = True to your storage class and it will not be touched to get image dimensions. Likely:

    from storages.backends.s3boto import S3BotoStorage
    S3BotoStorage.IGNORE_IMAGE_DIMENSIONS = True
    

    I still need to investigate where the numbers are used, to know if simple returning (0, 0) can lead to any problem, but no bug raised for now.

提交回复
热议问题