英文:
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文件,然后从那里提供它:
- 使用blobstore.Writer在blobstore中创建zip文件
- 使用blobstore.Send提供zip文件
这样,您还可以加快对相同zip文件/捆绑包的后续请求,因为您已经创建/存储了它。
英文:
You should probably create the zip file in the blobstore, then serve it from there:
- Create the zip in the blobstore utilizing blobstore.Writer
- Serve the zip using blobstore.Send
This way you can also speed up subsequent requests for the same zip/bundle as you will have already created/stored it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论