使用golang发送存储在Google Appengine blobstore blob中的图像。

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

golang: emailing an image stored as a Google Appengine blobstore blob

问题

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

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)

图像被正确地存储,即可以使用blobstore.Send(...)进行服务。

上述代码在某种程度上起作用(即它确实读取了blob数据),但它会将任何0x0a字节转换为0x0d 0x0a对(即LF转换为CR LF)。

在Go中是否有一种方法可以解决这个行为(而不是编写一个过滤器将0x0d0a转换回0x0a)?

编辑:

事实证明,问题并不是Blobstore.Reader本身,而是开发应用服务器上mail.py中的附件编码。

英文:

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.

答案1

得分: 0

开发应用程序服务器上的邮件附件处理未正确编码图像数据。如果已知附件数据是二进制的,则部分解决方法是在以下行之后添加一行代码:

encoders.encode_base64(mime_attachment)

在文件

> google/appengine/api/mail.py

中的以下行之后:

mime_attachment.set_payload(attachment.data())

对于图像内容类型,使用MIMEImage附件将是更好的解决方案,但会导致“LazyImporter对象不可调用”错误。

英文:

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.

huangapple
  • 本文由 发表于 2012年5月28日 02:03:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/10776103.html
匿名

发表评论

匿名网友

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

确定