0

I'm using Imagekit to resize pictures on a website i'm developing with Django, I've used Imagekit before without any issues but I have a strange problem.

I am using S3 for Media/Static files. When I upload an image in admin and refresh the template serving the images i get ValueError at / I/O operation on closed file error page. Then if I refresh the page it loads fine without a problem.

I am using an ImageSpec field to resize images and using {{ image.thumbnail.url }} in my template.

I am very much a beginner with Django so looking for some direction. Why will it work on a page reload and not when initially uploaded?

Here are the relevant fields in my model.

class Media(models.Model):

    
    image = models.ImageField(upload_to="media")
    thumbnail = ImageSpecField([Adjust(sharpness=1.1), ResizeToFill(800, 650)],
                               source='image', format='JPEG', options={'quality': 100})

I am assuming it's a problem with S3. It's trying to access the resized image but the Imagekit thumbnail isn't created on fileupload, only when the page is loaded?

Rob
  • 46
  • 3

1 Answers1

0

Ok, after some searching, this is the fix (and all now working) to solve the issue with storages which is the source of the problem.

from storages.backends.s3boto3 import S3Boto3Storage
from tempfile import SpooledTemporaryFile

class CustomS3Boto3Storage(S3Boto3Storage):
    location = 'media'
    file_overwrite = False

    """
    This is our custom version of S3Boto3Storage that fixes a bug in
    boto3 where the passed in file is closed upon upload.
    From:
    https://github.com/matthewwithanm/django-imagekit/issues/391#issuecomment-275367006
    https://github.com/boto/boto3/issues/929
    https://github.com/matthewwithanm/django-imagekit/issues/391
    """

    def _save(self, name, content):
        """
        We create a clone of the content file as when this is passed to
        boto3 it wrongly closes the file upon upload where as the storage
        backend expects it to still be open
        """
        # Seek our content back to the start
        content.seek(0, os.SEEK_SET)

        # Create a temporary file that will write to disk after a specified
        # size. This file will be automatically deleted when closed by
        # boto3 or after exiting the `with` statement if the boto3 is fixed
        with SpooledTemporaryFile() as content_autoclose:

            # Write our original content into our copy that will be closed by boto3
            content_autoclose.write(content.read())

            # Upload the object which will auto close the
            # content_autoclose instance
            return super(CustomS3Boto3Storage, self)._save(name, content_autoclose)```
Rob
  • 46
  • 3