清理AppEngine BlobStore

huangapple go评论105阅读模式
英文:

Cleaning up the AppEngine BlobStore

问题

我的AppEngine服务器中有很多未使用的BlobStore中的孤立blob。我想编写代码来遍历所有的blob,检查它们是否未被使用,然后进行删除。我找不到遍历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?

答案1

得分: 1

你可以通过数据存储查询列出 https://cloud.google.com/appengine/docs/go/blobstore/reference#BlobInfo(尽管此类查询是最终一致的)。

英文:

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

答案2

得分: 0

以下是用于在golang中迭代blob的代码解决方案:

c.Infof("迭代blob")
q := datastore.NewQuery("__BlobInfo__")

// 迭代结果
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("获取下一个Blob时出错:%v", err)
        break
    }
    // 对Blob bi进行操作
    c.Infof("获取到大小为[%v]的Blob [%v]", bi.Size, bi.ContentType)
    total++
    if total > 100 {
        break
    }
}
c.Infof("迭代完成")

您还需要使用以下函数来忽略字段不匹配错误

```go
func isErrFieldMismatch(err error) bool {
    _, ok := err.(*datastore.ErrFieldMismatch)
    return ok
}
英文:

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

}

huangapple
  • 本文由 发表于 2015年4月25日 00:26:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/29852719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定