【问题标题】:how to delete orphaned blob in google app engine如何在谷歌应用引擎中删除孤立的 blob
【发布时间】:2014-07-24 19:04:39
【问题描述】:

我一直在遵循此站点上的说明 - Is it possible to find and delete orphaned blobs in the app engine blobstore? - 关于如何删除孤立的 blob,但我一直收到错误消息。

from google.appengine.ext.blobstore import BlobInfo

#Here is where I store my blobKeys
class Content(ndb.Model):
  blobKey = ndb.BlobKeyProperty(required=False)

#Here is what I've been trying to follow
class Refresh(blobstore_handlers.BlobstoreUploadHandler):
  def get(self):
  blobs = BlobInfo.all().fetch(500)
  for blob in blobs:
    if not Content.all().filter("blob_ref =", blob.key()).count(1): #ERROR
      blob.delete()

我不断收到错误AttributeError: type object 'Content' has no attribute 'all'

海报提到“如果您的 BlobReferenceProperty 字段被索引,那么是的,这很有可能。”可能是我使用 ndb.BlobKeyProperty 而不是 BlobReferenceProperty 的问题吗?谢谢,阅读。

更新 参考Expected BlobKey but instead I get a BlobInfo object - How to get BlobKey from BlobInfo object?查看解决方案

【问题讨论】:

  • Google 正在远离 blobstore。 Files api 已被弃用。您可以使用不允许具有相同文件名的 blob 版本的 Google 云存储。
  • 谢谢 voscausa,不过我有一个问题。当谷歌说“文件 api 已被弃用”时,这是否意味着您将不再能够将图片上传到 blobstore?根据developers.google.com/appengine/docs/python/blobstore 看来blob_info = upload_files[0] 会没事的。
  • 换句话说,我的代码中根本没有使用from google.appengine.api import files
  • 是的,您仍然可以上传文件。如果应用程序将数据写入 blobstore,则仅需要 Files API。

标签: google-app-engine blobstore


【解决方案1】:

你混淆了 db 和 ndb apis

要使用 ndb 进行查询,请使用 query 方法

你的情况

blobs = BlobInfo.query().fetch(500)

但这并不完全正确。您应该将其重写为

for blob in BlobInfo.query(options=QueryOptions(batchsize=200))
   # do stuff

【讨论】:

  • 谢谢蒂姆,我正在阅读developers.google.com/appengine/docs/python/blobstore/…,但仍然无法让您的代码正常工作。我正在尝试self.response.out.write(str(blobs)) 只是想看看会发生什么,但什么也没有出现。当我尝试使用blobs = BlobInfo.query().fetch(500)时,我也会收到错误AttributeError: type object 'BlobInfo' has no attribute 'query'
  • 没关系,我才明白你的意思。 BlobInfo 是 MyModel。谢谢
  • 我只是转置了你的代码,并为 ndb 更正了它,我没有费心去深入它;-)
猜你喜欢
  • 2011-02-25
  • 1970-01-01
  • 2011-07-18
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-10
  • 1970-01-01
相关资源
最近更新 更多