如何提供Blobstorage图像的大型zip文件?

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

how to serve large zip files of Blobstorage images?

问题

我想提供一个动态的zip文件,其中包含存储在blobstorage中的多个用户上传的图像。

我用以下代码成功地实现了这一点,但是我遇到了一个问题,即Appengine实例被终止,因为它们消耗了太多的内存。

是否可能通过直接将这些zip文件流式传输到客户端而不将它们保存在内存中来提供这样的zip文件?是否有其他解决方案?

w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", "attachment;filename=photos.zip")

writer := zip.NewWriter(w)

defer writer.Close()

for _, key := range l.Files {
	info, err := blobstore.Stat(c, appengine.BlobKey(key))

	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return	
	}

	wr, err := writer.Create(info.Filename)

	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return	
	}

	reader := blobstore.NewReader(c, appengine.BlobKey(key))

	io.Copy(wr, reader)
}
英文:

I want to serve a dynamic zip file with multiple user uploaded images that are stored in blobstorage

I'm successfuly doing it with the following code, but I encounter a problem where the Appengine instances are being terminated because they consume too much memory.

is it possible to serve such zip files by streaming them directly to the client and not keeping them in the memory? is there another solution?

w.Header().Set("Content-Type", "application/zip")
w.Header().Set("Content-Disposition", "attachment;filename=photos.zip")

writer := zip.NewWriter(w)

defer writer.Close()

for _, key := range l.Files {
	info, err := blobstore.Stat(c, appengine.BlobKey(key))

	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return	
	}

	wr, err := writer.Create(info.Filename)

	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return	
	}

	reader := blobstore.NewReader(c, appengine.BlobKey(key))

	io.Copy(wr, reader)
}	

答案1

得分: 0

你应该在blobstore中创建zip文件,然后从那里提供它:

这样,您还可以加快对相同zip文件/捆绑包的后续请求,因为您已经创建/存储了它。

英文:

You should probably create the zip file in the blobstore, then serve it from there:

This way you can also speed up subsequent requests for the same zip/bundle as you will have already created/stored it.

huangapple
  • 本文由 发表于 2013年4月22日 20:05:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/16146725.html
匿名

发表评论

匿名网友

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

确定