【问题标题】:Doing Image transforms inside an UploadHandler and saving it to the Blobstore在 UploadHandler 中进行图像转换并将其保存到 Blobstore
【发布时间】:2013-08-10 23:58:52
【问题描述】:

我有一个用 Python 编写的关于 GAE 的页面,它 (1) 将 jpg 文件上传到 blobstore。那部分有效。我现在需要进行 (2) 我手气不错的图像转换,然后 (3) 将其作为另一个 blob 存储在 blobstore 中。理想情况下,我想在同一个上传处理程序中执行 (1)、(2) 和 (3)。

我已按照此处的代码进行操作,但它只执行 (1) 和 (2)。 https://developers.google.com/appengine/docs/python/images/#Python_Transforming_images_from_the_Blobstore

我浏览了 SO,我能找到的最接近的是: Storing Filtered images on the blobstore in GAE

它将转换保存到文件(使用 Files API),然后将文件上传到 blobstore。但是,它使用 Files api,并且根据以下内容,不推荐使用 Files API。 https://developers.google.com/appengine/docs/python/blobstore/#Python_Writing_files_to_the_Blobstore

在我的模型中,我有一个 BlobKeyProperty,用于存储对 blobstore 中图像的引用

class ImageModel(ndb.Model):
   imagetoserve =  ndb.BlobKeyProperty(indexed=False)

这是目前为止的上传处理程序代码:

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.api.images import get_serving_url
from google.appengine.api import images

upload_files = self.get_uploads('imgfile')  # 'file' is file upload field in the form
blob_info = upload_files[0]

imgtmp = images.Image(blob_info)
imgtmp.im_feeling_lucky()
img = ImageModel()
img.imagetoserve = imgtmp
img.put()

我的问题出在这一行:

img.imagetoserve = imgtmp

模型是一个 blobkey 属性,但我给它提供了一个图像,显然会导致类型不匹配的错误。如何执行将转换后的 imgtmp 上传到 blobstore、捕获 blobkey 并保存对我的模型的引用的步骤?

【问题讨论】:

    标签: python image google-app-engine google-cloud-datastore blobstore


    【解决方案1】:

    阅读Blobstore API Documentation

    不幸的是,传统上您会通过 Files API 完成此操作,但由于他们不赞成使用 GCS,因此您可以执行以下操作(您可以填写缺失的部分):(来自 this example

    import cloudstorage as gcs
    from google.appengine.ext import blobstore
    
    class ImageModel(ndb.Model):
        image_filename = ndb.StringProperty(indexed=False)
    
        @property
        def imagetoserve(self):
            return blobstore.create_gs_key(self.image_filename)
    
    BUCKET = "bucket_to_store_image\\"
    
    with gcs.open(BUCKET + blob_info.filename, 'w', content_type='image/png') as f:
        f.write(imgtmp.execute_transforms())
    
    img = ImageModel()
    img.image_filename = BUCKET + blob_info.filename
    img.put()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多