清理AppEngine BlobStore

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

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的代码解决方案:

  1. c.Infof("迭代blob")
  2. q := datastore.NewQuery("__BlobInfo__")
  3. // 迭代结果
  4. total := 0
  5. t := q.Run(c)
  6. for {
  7. var bi blobstore.BlobInfo
  8. _, err := t.Next(&bi)
  9. if err == datastore.Done {
  10. break
  11. }
  12. if err != nil && isErrFieldMismatch(err) == false {
  13. c.Errorf("获取下一个Blob时出错:%v", err)
  14. break
  15. }
  16. // 对Blob bi进行操作
  17. c.Infof("获取到大小为[%v]的Blob [%v]", bi.Size, bi.ContentType)
  18. total++
  19. if total > 100 {
  20. break
  21. }
  22. }
  23. c.Infof("迭代完成")
  24. 您还需要使用以下函数来忽略字段不匹配错误
  25. ```go
  26. func isErrFieldMismatch(err error) bool {
  27. _, ok := err.(*datastore.ErrFieldMismatch)
  28. return ok
  29. }
英文:

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

  1. c.Infof("Iterating over blobs")
  2. q := datastore.NewQuery("__BlobInfo__")
  3. // Iterate over the results.
  4. total := 0
  5. t := q.Run(c)
  6. for {
  7. var bi blobstore.BlobInfo
  8. _, err := t.Next(&bi)
  9. if err == datastore.Done {
  10. break
  11. }
  12. if err != nil && isErrFieldMismatch(err) == false {
  13. c.Errorf("Error fetching next Blob: %v", err)
  14. break
  15. }
  16. // Do something with the Blob bi
  17. c.Infof("Got blob [%v] of size [%v]", bi.ContentType, bi.Size)
  18. total++
  19. if total > 100 { break }
  20. }
  21. c.Infof("Iterating Done")

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

  1. func isErrFieldMismatch(err error) bool {
  2. _, ok := err.(*datastore.ErrFieldMismatch)
  3. 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:

确定