【问题标题】:Can't get blob using BlobReferenceProperty无法使用 BlobReferenceProperty 获取 blob
【发布时间】:2013-09-08 17:26:58
【问题描述】:

我有一个由StringBlobReferenceProperty 组成的实体:

class NameKeyPair(db.Model):
    human_readable_name = db.StringProperty()   
    blobkey = blobstore.BlobReferenceProperty()

当我在 blob 中保存一些文本时,我将 blobkey 设置为:

...
#Create the file
file_name = files.blobstore.create(mime_type='text/plain')

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

#Finalize the file
files.finalize(file_name)

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

#store it in DB
nameKeyPair = NameKeyPair()
nameKeyPair.blobkey = blobkey
nameKeyPair.human_readable_name = human_readable_name
nameKeyPair.put()
...

human_readable_name 是我发送给用户的字符串。现在,当用户将名称发回给我时,我想返回 blob 的文本:

...
human_readable_name = self.request.get('human_readable_name') 
q = NameKeyPair.all()
q.filter("human_readable_name =", human_readable_name)

blobkey = ""
for p in q.run(limit=1):
    blobkey = p.blobkey

blob_info = blobstore.BlobInfo.get(blobkey)
self.send_blob(blob_info)

这不起作用。 Appspot 只是说“服务器错误”。

如果我打印出我的变量 blobkey 它会显示 <google.appengine.ext.blobstore.blobstore.BlobInfo object at 0x54dbe8bfa91b53a0> 它不应该是一个 blobkey 吗?

此页面:https://developers.google.com/appengine/docs/python/blobstore/blobkeyclass 说“您可以通过将密钥传递给 BlobInfo.get() 类方法来获取带有 BlobKey 的 BlobInfo 实体。”

这就是我将 blobkey 传递给 BlobInfo.get 的原因。但如果我的“blobkey”实际上已经是BlobInfo 对象,也许我不需要这样做。我尝试将其直接传递给send_blob

self.send_blob(blobkey)

但我得到了相同的结果。

并基于此页面:https://developers.google.com/appengine/docs/python/blobstore/ 显示代码示例:self.send_blob(blob_info) 似乎这应该就是我所需要的......但它只是不起作用。有任何想法吗?

【问题讨论】:

  • 日志中有更详细的错误吗?

标签: python google-app-engine blobstore


【解决方案1】:

我无法评论,但尝试使用 [object].key() 我发现它有点迟钝,什么时候是对象的键,什么时候是字符串

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-23
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多