GAE - 将优化的图像上传到云存储

时间:2015-03-17 07:19:26

标签: python google-app-engine google-cloud-storage image-optimization

我正在开发一个简单的应用程序,它可以优化图片并将其保存在云存储中。我找到了一个获取文件并使用PIL优化它的示例。代码如下所示:

def inPlaceOptimizeImage(photo_blob):
        blob_key = photo_blob.key()
        new_blob_key = None

        img = Image.open(photo_blob.open())
        output = StringIO.StringIO()
        img.save(output,img.format, optimized=True,quality=90)
        opt_img = output.getvalue()
        output.close()

        # Create the file
        file_name = files.blobstore.create(mime_type=photo_blob.content_type)

        # Open the file and write to it
        with files.open(file_name, 'a') as f:
            f.write(opt_img)

        # Finalize the file. Do this before attempting to read it.
        files.finalize(file_name)

        # Get the file's blob key
        return files.blobstore.get_blob_key(file_name)

这在本地工作正常(虽然我不知道它的优化程度如何,因为当我通过http://www.jpegmini.com/之类的内容运行上传的图像时,它仍会减少2.4倍。但是,当我部署应用程序并尝试上传图像时,我经常会在日志中收到500条错误和这些消息:

F 00:30:33.322 Exceeded soft private memory limit of 128 MB with 156 MB after servicing 7 requests total
W 00:30:33.322 While handling this request, the process that handled this request was found to be using too much memory and was terminated. This is likely to cause a new process to be used for the next request to your application. If you see this message frequently, you may have a memory leak in your application.

我有两个问题:

  1. 这是在云存储中优化和保存图像的最佳方式吗?
  2. 如何防止发生这500个错误?
  3. 提前致谢。

1 个答案:

答案 0 :(得分:1)

由于Instance类的内存限制,您遇到的错误正在发生。

我建议您编辑.yaml文件以配置模块,并将您的Instance类指定为F2或更高版本。 如果您不使用模块,还应在app.yaml文件的开头添加“module:default”,让GAE知道这是您的默认模块。

您可以从文档中查看此article,以查看可用的不同Instance类以及轻松配置它们的方法。

另一个更基本的解决方法是在上传时限制图像大小,但最终会遇到类似的问题。

关于以前的问题以及优化图像的方法,您可能需要查看App Engine Images API,它提供了使用专用图像服务处理图像数据的功能。在你的情况下,你可能会喜欢"我感觉幸运"转型。通过使用此API,您可能不需要更新Instance类。

相关问题