英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论