Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用boto/django存储和amazon s3时从图像生成缩略图时出现问题_Django_Amazon S3_Boto_Django Storage - Fatal编程技术网

使用boto/django存储和amazon s3时从图像生成缩略图时出现问题

使用boto/django存储和amazon s3时从图像生成缩略图时出现问题,django,amazon-s3,boto,django-storage,Django,Amazon S3,Boto,Django Storage,我使用这里找到的要点在保存时生成缩略图。它在本地工作,但在生产中不起作用,在生产中我会得到一个AttributeError: fp is at EOF. Use rewind option or seek() to data start. 我已经用了100种不同的方法来处理这个问题,在我能想到的几乎每个地方添加image.seek(0)或suf.seek(0)或temp_handle.seek(0),但没有任何效果 class Image(Media): image = models

我使用这里找到的要点在保存时生成缩略图。它在本地工作,但在生产中不起作用,在生产中我会得到一个AttributeError:

 fp is at EOF. Use rewind option or seek() to data start.
我已经用了100种不同的方法来处理这个问题,在我能想到的几乎每个地方添加image.seek(0)或suf.seek(0)或temp_handle.seek(0),但没有任何效果

class Image(Media):
    image = models.ImageField(
        upload_to=image_folder
    )

    thumbnail = models.ImageField(
        upload_to=thumbnail_folder,
        max_length=500,
        null=True,
        blank=True
    )

    def create_thumbnail(self):
        # original code for this method came from
        # http://snipt.net/danfreak/generate-thumbnails-in-django-with-pil/

        # If there is no image associated with this.
        # do not create thumbnail
        if not self.image:
            return

        from PIL import Image
        from cStringIO import StringIO
        from django.core.files.uploadedfile import SimpleUploadedFile
        import os

        # Set our max thumbnail size in a tuple (max width, max height)
        THUMBNAIL_SIZE = (99, 66)

        DJANGO_TYPE = self.image.file.content_type

        if DJANGO_TYPE == 'image/jpeg':
            PIL_TYPE = 'jpeg'
            FILE_EXTENSION = 'jpg'
        elif DJANGO_TYPE == 'image/png':
            PIL_TYPE = 'png'
            FILE_EXTENSION = 'png'

        # Open original photo which we want to thumbnail using PIL's Image
        image = Image.open(StringIO(self.image.read()))

        # We use our PIL Image object to create the thumbnail, which already
        # has a thumbnail() convenience method that contrains proportions.
        # Additionally, we use Image.ANTIALIAS to make the image look better.
        # Without antialiasing the image pattern artifacts may result.
        image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)

        # Save the thumbnail
        temp_handle = StringIO()
        image.save(temp_handle, PIL_TYPE)
        temp_handle.seek(0)

        # Save image to a SimpleUploadedFile which can be saved into
        # ImageField
        suf = SimpleUploadedFile(os.path.split(self.image.name)[-1],
                temp_handle.read(), content_type=DJANGO_TYPE)
        # Save SimpleUploadedFile into image field
        self.thumbnail.save(
            '%s_thumbnail.%s' % (os.path.splitext(suf.name)[0], FILE_EXTENSION),
            suf,
            save=False
        )

    def save(self, *args, **kwargs):

        self.create_thumbnail()

        force_update = False

        # If the instance already has been saved, it has an id and we set 
        # force_update to True
        if self.id:
            force_update = True

        # Force an UPDATE SQL query if we're editing the image to avoid integrity exception
        super(Image, self).save(force_update=force_update)

读这个:我读过。OP通过在文件保存前添加image_file.seek(0)解决了他的问题,但这对我不起作用。还有人建议添加rewind=True以设置来自文件的内容,但这似乎与此无关,或者如果是,我不知道这段代码在哪里。他修改了django存储并添加了rewind=True。在django存储中,我从_文件中找到set_contents_的唯一位置是s3boto.py文件和gs.py文件,在这两个文件中,rewind=True.ok。找到了。贝托的钥匙。谢谢