无效的字符串键BadKeyError

2024-07-01 08:24:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试创建一个脚本来提供来自GAE数据存储的图像时遇到了这个错误。在

  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/api/datastore_types.py", line 379, in __init__
    raise datastore_errors.BadKeyError('Invalid string key %s.' % encoded)
BadKeyError: Invalid string key 5066549580791808.

使用此代码:

^{pr2}$

这个类是从GAE上的image documentation创建的


Tags: 数据key图像脚本string错误googlecontents
2条回答

另一个选择是使用Blobstore API

这样,您就可以使用imagesapi动态地为不同大小的图像提供服务:Transforming images from the Blobstore

import webapp2
from google.appengine.api import images
from google.appengine.ext import blobstore

    class Thumbnailer(webapp2.RequestHandler):
        def get(self):
            blob_key = self.request.get("blob_key")
            if blob_key:
                blob_info = blobstore.get(blob_key)

                if blob_info:
                    img = images.Image(blob_key=blob_key)
                    img.resize(width=80, height=100)
                    img.im_feeling_lucky()
                    thumbnail = img.execute_transforms(output_encoding=images.JPEG)

                    self.response.headers['Content-Type'] = 'image/jpeg'
                    self.response.out.write(thumbnail)
                    return

            # Either "blob_key" wasn't provided, or there was no value with that ID
            # in the Blobstore.
            self.error(404)

5066549580791808不是钥匙,是身份证

您需要使用SomeModel.get()db.get()需要整个密钥,其中包括模型和路径。在

相关问题 更多 >

    热门问题