Blobstore阅读器不会读取大图像

问题描述:

I am trying to read images from the Blobstore from the Go server side code. But if the image is large (as in: 0.5MB - 1.7MB) a large portion of the byte buffer becomes 0 which breaks the image.

The image works if I use serveUrl, but this is not an option for me in this case.

My first thought was that there was a size limit to the read, found this:

In addition to systemwide safety quotas, a limit applies specifically to the use of the Blobstore: the maximum size of Blobstore data that can be read by the application with one API call is 32 megabytes.

The images I read are nowhere near 32MB.

Function I use to read from the Blobstore:

func BlobAsBase64(c appengine.Context, blobKey string) (*blobstore.BlobInfo, string, error) {
    info, err := blobstore.Stat(c, appengine.BlobKey(blobKey))
    if err != nil {
        return nil, "", err
    }

    imageBuffer := make([]byte, info.Size)
    reader := blobstore.NewReader(c, appengine.BlobKey(blobKey))
    if _, err = reader.Read(imageBuffer); err != nil {
        return nil, "", err
    }

    imageBase64 := base64.StdEncoding.EncodeToString(imageBuffer)

    return info, imageBase64, nil
}

What is the reason that my images become broken when I read them from the Blobstore?

我正在尝试从Go服务器端代码从Blobstore读取图像。 但是,如果图像很大(例如:0.5MB-1.7MB),则字节缓冲区的很大一部分将变为0,这会破坏图像。 p>

如果我使用serveUrl,则图像可以工作,但是 在这种情况下,这不是我的选择。 p>

我首先想到的是,读取的找到了这个: p>

除了系统范围内的安全配额外,还有一个限制 特别适用于Blobstore的使用:应用程序一次调用一个API可以读取的Blobstore数据的最大大小为32 MB。 p> blockquote>

我读取的图像远不超过32MB。 p>

我用来从Blobstore读取的函数: p>

  func BlobAsBase64(c appengine。 上下文,blobKey字符串)(* blobstore.BlobInfo,字符串,错误){
 info,err:= blobstore.Stat(c,appengine.BlobKey(blobKey))
如果err!= nil {
返回nil,“  “,err 
} 
 
i  mageBuffer:= make([] byte,info.Size)
 reader:= blobstore.NewReader(c,appengine.BlobKey(blobKey))
如果_,err = reader.Read(imageBuffer);  err!= nil {
返回nil,“”,err 
} 
 
 imageBase64:= base64.StdEncoding.EncodeToString(imageBuffer)
 
返回信息,imageBase64,nil 
} 
  code  >  pre> 
 
 

从Blob存储区读取图像时,图像损坏是什么原因? p> div>

I conjecture the reason this isn't working is because the reader.Read method is returning before it has filled the buffer. See the contract for io.Reader

Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Even if Read returns n < len(p), it may use all of p as scratch space during the call. If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more.

Note the last sentence in particular.

Instead of reader.Read(imageBuffer) try using ioutil.ReadAll which should fix your problem.