假设该应用收到了一条消息has attachments(mail_message.attachments
)。现在,我想将消息保存在数据存储区中。我不想在那里存储附件,所以我想只保留blobstore密钥。我知道我可以write files to blobstore。我的问题是:
BlobKeyProperty()
在这种情况下不起作用)。更新。对于(1)the following code可以使用:
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)))
答案 0 :(得分:0)
您应该使用NDB而不是(旧)数据存储区。在NDB中,您可以使用重复和结构化重复属性来保存BlobProperties和文件名列表。
请参阅:https://developers.google.com/appengine/docs/python/ndb/properties
答案 1 :(得分: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_key
以ndb.BlobKeyProperty(repeated=True)
的形式存储在数据存储区中。