【问题标题】:send data from blobstore as email attachment in GAE在 GAME 中将数据从 Blob 存储作为电子邮件附件发送
【发布时间】:2013-02-20 11:48:32
【问题描述】:

为什么下面的代码不起作用?收到电子邮件,并且文件带有正确的文件名(它是一个 .png 文件)。但是当我尝试打开文件时,它无法正确打开(Win​​dows Gallery 报告它can't open this photo or videothe file may be unsupported, damaged or corrupted)。

当我使用 blobstore_handlers.BlobstoreDownloadHandler 的子类(基本上是 GAE 文档中的确切处理程序)和相同的 blob 键下载文件时,一切正常,Windows 读取图像。

还有一点信息 - 下载的二进制文件和电子邮件看起来非常相似,但长度略有不同。

有人对如何获取从 GAE blobstore 发送的电子邮件附件有任何想法吗? S/O上也有类似的问题,说明其他人也有这个问题,但似乎没有任何结论。

from google.appengine.api import mail
from google.appengine.ext import blobstore

def send_forum_post_notification():
blob_reader = blobstore.BlobReader('my_blobstore_key')
blob_info = blobstore.BlobInfo.get('my_blobstore_key')
value = blob_reader.read()
mail.send_mail(
    sender='my.email@address.com',
    to='my.email@address.com',
    subject='this is the subject',
    body='hi',
    reply_to='my.email@address.com',
    attachments=[(blob_info.filename, value)]
)

send_forum_post_notification()

【问题讨论】:

    标签: python google-app-engine blobstore


    【解决方案1】:

    我不明白您为什么使用元组作为附件。我用:

    message = mail.EmailMessage(sender = ......
    message.attachments = [blob_info.filename,blob_reader.read()]
    

    【讨论】:

    • 我使用元组是因为引用文档,send_mail 中的attachments 字段对应于The file attachments for the message, as a list of two-value tuples, one tuple for each attachment. Each tuple contains a filename as the first element, and the file contents as the second element.
    【解决方案2】:

    我发现这段代码在 dev_appserver 上不起作用,但在推送到生产环境时确实起作用。

    【讨论】:

      【解决方案3】:

      我在 Python Google App Engine 应用程序上使用 blobstore 时遇到了类似的问题。我的应用程序处理的是 PDF 文件而不是图像,但我也看到了“文件可能不受支持、损坏或损坏”的错误,使用的代码类似于上面显示的代码。

      尝试以这种方式解决问题:在读取二进制流之前对 BlobInfo 对象调用 open()。替换这一行:

      value = blob_reader.read()
      

      ...用这两行:

      bstream = blob_info.open()
      value = bstream.read()
      

      那么你也可以删除这一行:

      blob_reader = blobstore.BlobReader('my_blobstore_key')
      

      ...因为上面的 bstream 将是 BlobReader 类型。

      来自 Google 的相关文档位于此处: https://cloud.google.com/appengine/docs/python/blobstore/blobinfoclass#BlobInfo_filename

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-31
        • 2015-09-09
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 2011-11-02
        • 1970-01-01
        相关资源
        最近更新 更多