清理AppEngine BlobStore

问题描述:

My AppEngine server has a lot of orphaned blobs not used in the BlobStore. I'd like to write code to iterate over all the blobs and check if they are not being used and then delete. I can't find a way to iterate over the BlobStore. Is this possible?

我的AppEngine服务器有很多BlobStore中未使用的孤立Blob。 我想编写代码来遍历所有Blob,并检查它们是否未被使用,然后删除。 我找不到迭代BlobStore的方法。 可以吗? p> div>

You can list the https://cloud.google.com/appengine/docs/go/blobstore/reference#BlobInfo via a datastore query (though such query is eventually consistent).

Here is a code solution for iterating over blobs in golang:

c.Infof("Iterating over blobs")
q := datastore.NewQuery("__BlobInfo__")

// Iterate over the results.
total := 0
t := q.Run(c)
for {
        var bi blobstore.BlobInfo
        _, err := t.Next(&bi)
        if err == datastore.Done {
                break
        }
        if err != nil && isErrFieldMismatch(err) == false {
                c.Errorf("Error fetching next Blob: %v", err)
                break
        }
        // Do something with the Blob bi
        c.Infof("Got blob [%v] of size [%v]", bi.ContentType, bi.Size)
        total++
        if total > 100 { break }
}
c.Infof("Iterating Done")

You'll also need to use this function to ignore field mismatch errors:

func isErrFieldMismatch(err error) bool {
    _, ok := err.(*datastore.ErrFieldMismatch)
    return ok

}