Django手动文件上传到s3次 - 502

时间:2014-09-19 23:42:46

标签: python django image file-io amazon-s3

我在网站的管理面板中有一个脚本,允许我们上传图片,裁剪并保存。保存时,会裁剪并上传原始图像。我们使用cuddlybuddly.storage.s3.S3Storage作为我们的存储后端。

以下是上传图片的块:

    name, contents = self.crop_image(x, y, w, h, (220, 142), 'default_filter')
    if name is not None and contents is not None:
        self.default_filter_image.save(
                    name,
                    ContentFile(contents.getvalue()), save=False
            )

裁剪图像几乎没有时间,502在尝试调用self.default_filter_image.save()位时发生。我们上传的图片不到100k(微小)。

这是crop_image函数,以防出现问题。

def crop_image(self, x, y, w, h, finished_size, img_prop):
    """
    Returns a cropped version of an image based on some size settings that are passed in. Used to
    create all the sizes and can be used for any size crop.
    """
    original = None

    if self.original_image:
        self.original_image.seek(0)
        original = Image.open(self.original_image)

    if original is not None and x is not None and y is not None \
                    and w is not None and h is not None:

        orig = original.copy()

        cropped = orig.crop(
                # left, upper, right, lower
                (x, y, (x + w), (y + h))
        )

        cropped = cropped.resize(finished_size, Image.ANTIALIAS)

        tmp = Image.new('RGB', cropped.size)
        tmp.paste(cropped, (0, 0))
        cropped = tmp

        contents = StringIO()
        cropped.save(contents, format='jpeg', quality=90)
        contents.seek(0)
        filename = '%s_%s.jpg' % (str(uuid.uuid4()), img_prop)

        return (filename, contents)

    return (None, None)

有没有人知道我怎么可以调试它?

1 个答案:

答案 0 :(得分:1)

CuddlyBuddly过时并且每次想要发布一个文件时都会查询存储桶中的整个文件列表,这最终会减慢并完全停止工作。使用s3Boto的django-storages应用程序更新,效果很好。

https://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html