英文:
How do I delete all blobs in app engine on Go?
问题
blobstore API没有列出所有blob的功能。我该如何获取这个列表,然后删除所有的blob呢?
英文:
The blobstore API has no function to list all blobs. How can I get this list and then delete all blobs?
答案1
得分: 5
在Go语言的App Engine上,blobstore API没有提供这样的功能。相反,可以使用datastore来获取__BlobInfo__
实体,然后将其转换为appengine.BlobInfo
。尽管API声称有一个BlobKey字段,但实际上它没有被填充。相反,可以使用返回的键的字符串ID,并将其转换为appengine.BlobKey
,然后将其传递给blobstore.Delete
函数。
下面是一个位于"/tasks/delete-blobs"路径的处理器,用于循环删除20000个blob,直到它们全部被删除。请注意,这里没有使用游标。我怀疑__BlobInfo__
是特殊的,不支持游标。(当我尝试使用游标时,它们没有起作用。)
func DeleteBlobs(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
c = appengine.Timeout(c, time.Minute)
q := datastore.NewQuery("__BlobInfo__").KeysOnly()
it := q.Run(c)
wg := sync.WaitGroup{}
something := false
for _i := 0; _i < 20; _i++ {
var bk []appengine.BlobKey
for i := 0; i < 1000; i++ {
k, err := it.Next(nil)
if err == datastore.Done {
break
} else if err != nil {
c.Errorf("err: %v", err)
continue
}
bk = append(bk, appengine.BlobKey(k.StringID()))
}
if len(bk) == 0 {
break
}
go func(bk []appengine.BlobKey) {
something = true
c.Errorf("deleting %v blobs", len(bk))
err := blobstore.DeleteMulti(c, bk)
if err != nil {
c.Errorf("blobstore delete err: %v", err)
}
wg.Done()
}(bk)
wg.Add(1)
}
wg.Wait()
if something {
taskqueue.Add(c, taskqueue.NewPOSTTask("/tasks/delete-blobs", nil), "")
}
}
希望对你有帮助!
英文:
The blobstore API on appengine for go has no way to do this. Instead, use the datastore to fetch __BlobInfo__
entities as appengine.BlobInfo
. Although the API claims to have a BlobKey field, it is not populated. Instead, use the string ID of the returned key and cast it to an appengine.BlobKey
, which you can then pass to blobstore.Delete
.
Here's a handler at "/tasks/delete-blobs" to delete 20k blobs at a time in a loop until they are all deleted. Also note that cursors are not used here. I suspect that __BlobInfo__
is special and doesn't support cursors. (When I attempted to use them, they did nothing.)
func DeleteBlobs(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
c = appengine.Timeout(c, time.Minute)
q := datastore.NewQuery("__BlobInfo__").KeysOnly()
it := q.Run(ctx)
wg := sync.WaitGroup{}
something := false
for _i := 0; _i < 20; _i++ {
var bk []appengine.BlobKey
for i := 0; i < 1000; i++ {
k, err := it.Next(nil)
if err == datastore.Done {
break
} else if err != nil {
c.Errorf("err: %v", err)
continue
}
bk = append(bk, appengine.BlobKey(k.StringID()))
}
if len(bk) == 0 {
break
}
go func(bk []appengine.BlobKey) {
something = true
c.Errorf("deleteing %v blobs", len(bk))
err := blobstore.DeleteMulti(ctx, bk)
if err != nil {
c.Errorf("blobstore delete err: %v", err)
}
wg.Done()
}(bk)
wg.Add(1)
}
wg.Wait()
if something {
taskqueue.Add(c, taskqueue.NewPOSTTask("/tasks/delete-blobs", nil), "")
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论