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

问题描述:

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)
}   

我想为动态zip文件提供多个用户上传的图像,这些图像存储在blobstorage中 p>

我使用以下代码成功完成了此操作,但是我遇到了一个Appengine实例因消耗过多内存而被终止的问题。 p>

是否有可能 通过将此类zip文件直接流传输到客户端而不将其保留在内存中来提供这些zip文件? 还有另一种解决方案吗? p>

  w.Header()。Set(“ Content-Type”,“ application / zip”)
w.Header()。Set(“ Content  -处置”,“附件;文件名= photos.zip”)
 
writer:= zip.NewWriter(w)
 
defer writer.Close()
 
对于_,键:=范围l.Files {\  n info,err:= blobstore.Stat(c,appengine.BlobKey(key))
 
如果err!= nil {
 http.Error(w,err.Error(),http.StatusInternalServerError)
返回 
} 
 
 wr,err:= writer.Create(info.Filename)
 
 if err!= nil {
 http.Error(w,err.Error(),http.StatusInternalServerError)
 返回
} 
 
读者:= blobstore.NewReader(c,appengine.BlobKey(key))
 
 io.Copy(wr,reader)
} 
  code>  pre> \  n  div>

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.