1

假设应用收到一条带有附件( mail_message.attachments) 的消息。现在,我想将消息保存在数据存储中。我不想在那里存储附件,所以我想只保留 blobstore 密钥。我知道我可以将文件写入 blobstore。我的问题:

  1. 如何从邮件附件中提取文件;
  2. 如何保留原始文件名;
  3. 如何在数据存储中存储 blob 密钥(考虑到一封邮件可以包含多个附件BlobKeyProperty(),在这种情况下似乎不起作用)。

更新。对于 (1) ,可以使用以下代码:

my_file = []
my_list = []
if hasattr(mail_message, 'attachments'):
    file_name = ""
    file_blob = ""
    for filename, filecontents in mail_message.attachments:
        file_name = filename
        file_blob = filecontents.decode()
        my_file.append(file_name)
        my_list.append(str(store_file(self, file_name, file_blob)))
4

2 回答 2

0

您应该使用 NDB 而不是(旧)数据存储。在 NDB 中,您可以使用重复和结构化的重复属性来保存 BlobProperties 和文件名列表。

请参阅:https ://developers.google.com/appengine/docs/python/ndb/properties

于 2013-03-31T21:41:13.857 回答
0

这是我最终要做的:

class EmailHandler(webapp2.RequestHandler):
    def post(self):
        '''
        Receive incoming e-mails
        Parse message manually
        '''
        msg = email.message_from_string(self.request.body) # http://docs.python.org/2/library/email.parser.html
        for part in msg.walk():
            ctype = part.get_content_type()
            if ctype in ['image/jpeg', 'image/png']:
                image_file = part.get_payload(decode=True)
                image_file_name = part.get_filename()
                # save file to blobstore
                bs_file = files.blobstore.create(mime_type=ctype, _blobinfo_uploaded_filename=image_file_name)
                with files.open(bs_file, 'a') as f:
                    f.write(image_file)
                files.finalize(bs_file)
                blob_key = files.blobstore.get_blob_key(bs_file)

blob_keys 在数据存储中存储为ndb.BlobKeyProperty(repeated=True)

于 2013-12-22T11:46:26.747 回答