我想使用 python 将我的电子邮件附件推送到 s3 存储桶,我的以下代码执行该任务,但是当新电子邮件以不同的文件名命中时,这不起作用。
我想将任何动态名称和扩展名上传到 s3 存储桶。
我是Python中的新蜜蜂。
提前致谢。
# Write the attachment to a temp location
open('/tmp/filename.csv', 'wb').write(attachment.get_payload(decode=True))
您可以使用以下代码从 lambda 中提取附件:
def get_attachment(msg, content_type):
"""
Moves through a tree of email Messages to find an attachment.
:param msg: An email Message object containing an attachment in its Message tree
:param content_type: The type of attachment that is being searched for
:return: An email Message object containing base64 encoded contents (i.e. the attachment)
"""
attachment = None
msg_content_type = msg.get_content_type()
if ((msg_content_type == content_type or msg_content_type == 'text/plain')
and is_base64(msg.get_payload())):
attachment = msg
elif msg_content_type.startswith('multipart/'):
for part in msg.get_payload():
attachment = get_attachment(part, content_type)
attachment_content_type = attachment.get_content_type()
if (attachment and (attachment_content_type == content_type
or attachment_content_type == 'text/plain')
and is_base64(attachment.get_payload())):
break
else:
attachment = None
return attachment
其描述如下:https://medium.com/caspertechteam/processing-email-attachments-with-aws-a35a1411a0c4