如何在Go中删除App Engine中的所有blob?

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

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(&quot;__BlobInfo__&quot;).KeysOnly()
	it := q.Run(ctx)
	wg := sync.WaitGroup{}
	something := false
	for _i := 0; _i &lt; 20; _i++ {
		var bk []appengine.BlobKey
		for i := 0; i &lt; 1000; i++ {
			k, err := it.Next(nil)
			if err == datastore.Done {
				break
			} else if err != nil {
				c.Errorf(&quot;err: %v&quot;, err)
				continue
			}
			bk = append(bk, appengine.BlobKey(k.StringID()))
		}
		if len(bk) == 0 {
			break
		}
		go func(bk []appengine.BlobKey) {
			something = true
			c.Errorf(&quot;deleteing %v blobs&quot;, len(bk))
			err := blobstore.DeleteMulti(ctx, bk)
			if err != nil {
				c.Errorf(&quot;blobstore delete err: %v&quot;, err)
			}
			wg.Done()
		}(bk)
		wg.Add(1)
	}
	wg.Wait()
	if something {
		taskqueue.Add(c, taskqueue.NewPOSTTask(&quot;/tasks/delete-blobs&quot;, nil), &quot;&quot;)
	}
}

huangapple
  • 本文由 发表于 2014年1月26日 12:36:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/21359758.html
匿名

发表评论

匿名网友

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

确定