golang:通过电子邮件发送存储为Google Appengine blobstore blob的图像

golang:通过电子邮件发送存储为Google Appengine blobstore blob的图像

问题描述:

I'm trying to read a JPEG file stored in the GAE blobstore back into a byte array using the following code:

info,_  := blobstore.Stat(context,appengine.BlobKey(request.FormValue("blobkey")))
image   := make([]byte,info.Size)
reader  := blobstore.NewReader(context,appengine.BlobKey(request.FormValue("blobkey")))
n,nerr  := reader.Read(image)

The image is stored correctly i.e. it can be served using blobstore.Send(...).

And the above code sort of works (in that it does read back the blob data) but it converts any 0x0a byte into a 0x0d 0x0a pair (i.e. LF into CR LF).

Is there a way in Go to work around this behaviour (without writing a filter to convert 0x0d0a back to 0x0a) ?

EDIT:

It turns out the problem is not with Blobstore.Reader at all, but with the attachment encoding in mail.py on the dev appserver.

我正在尝试使用以下代码将存储在GAE blobstore中的JPEG文件读回到字节数组: p>

  info,_:= blobstore.Stat(context,appengine.BlobKey(request.FormValue(“ blobkey”)))
image:= make([] byte,info  .Size)
reader:= blobstore.NewReader(context,appengine.BlobKey(request.FormValue(“ blobkey”)))
n,nerr:= reader.Read(image)
  code>  pre> \  n 
 

图像已正确存储,即可以使用blobstore.Send(...)进行投放。 p>

以上代码的工作原理是(确实读取了 返回blob数据),但是它将任何0x0a字节转换为0x0d 0x0a对(即LF转换为CR LF)。 p>

Go中是否有一种方法可以解决此问题(无需编写 过滤器将0x0d0a转换回0x0a)吗? p>

编辑: p>

事实证明问题根本不在于Blobstore.Reader,而在于 开发应用程序服务器上mail.py中的附件编码。 p> div>

The mail attachment handling on the dev appserver does not correctly encode image data. If the attachment data is known to be binary, a partial workaround is to add the line:

encoders.encode_base64(mime_attachment)

after the line

mime_attachment.set_payload(attachment.data())

in the file

google/appengine/api/mail.py

Using a MIMEImage attachment for an image content type would be a better solution but causes a 'LazyImporter object is not callable' error.