【问题标题】:Including Google Cloud SDK in virtualenv using Python Flexible Environment使用 Python 灵活环境在 virtualenv 中包含 Google Cloud SDK
【发布时间】:2017-12-27 12:34:35
【问题描述】:

我想使用 google.appengine.api 图像包,但我不知道如何安装 virtualenv 工具。当我在正常环境中使用 dev_appserver.py 时,该程序包工作正常,但是当我将灵活环境与烧瓶一起使用时,它找不到该程序包。有没有办法将图像库添加到我的 virtualenv 中?

当我在将图像上传到服务器之前尝试使用 Pillow 调整图像大小时,但当我这样做时,图像会以 0B 的速度到达云存储。

if file and allowed_file(file.filename):
    filename = '%s_%s.jpg' % (item.id, len(item.photos))
    # Resize file using pillow
    image = Image.open(file)
    image.thumbnail((300,300)
    resized_image = io.BytesIO()
    image.save(resized_image, format='JPEG')
    # if I did a image.show() here the image would 
    # properly be shown resized
    gcs = storage.Client()
    bucket = gcs.get_bucket(CLOUD_STORAGE_BUCKET)
    blob = bucket.blob(filename)

    blob.upload_from_file(resized_image,
        content_type=file.content_type)
    # I would then view the image in the bucket and it shows up as 0 bytes
    # and blank
    # If I just use the regular file it uploads fine.

【问题讨论】:

  • 你能检查resized_image是否包含你所期望的吗?
  • 可能是blob.upload_from_file(resized_image.get_value(),...?
  • 尝试使用真实文件而不是io.BytesIO。如果可行,那显然是io.BytesIO 匹配问题,这是我怀疑的。我无法测试自己,因为我既没有使用 flex env 也没有使用烧瓶......
  • 如果您使用f 的句柄呼叫blob.upload_from_file()

标签: python google-app-engine google-cloud-platform virtualenv pillow


【解决方案1】:

您可能不走运,图像服务在标准环境之外不可用。

来自Migrating Services from the Standard Environment to the Flexible Environment

图像服务在标准之外不可用 环境。但是,您可以轻松地直接从您的 应用程序或直接从 Cloud Storage。

如果你需要做图像处理,你可以安装和使用任何图像 处理库如Pillow

图像服务还提供了避免动态 通过使用 服务网址。如果你想要类似的功能,你可以生成 提前调整图像大小并将其上传到 Cloud Storage 服务。或者,您可以使用第三方内容交付 提供图像大小调整的网络 (CDN) 服务。

有关更多资源,请参阅以下指南:

【讨论】:

  • 感谢您的回答。你完全回答了我。我已经编辑了这个问题并说明了当我使用 Pillow 时会发生什么。你能帮忙解决这部分问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-02
  • 1970-01-01
  • 1970-01-01
  • 2018-12-25
  • 1970-01-01
  • 2017-09-30
  • 2018-09-01
相关资源
最近更新 更多