从电子邮件中提取附件并将其保存(通过SES)到AWS S3中 [英] Extract and save attachment from email (via SES) into AWS S3
问题描述
我想从电子邮件中提取附件并将其保存到我的新S3存储桶中.到目前为止,我已经配置了AWS Simple Email Service来拦截传入的电子邮件.现在,我有一个AWS lambda python函数,该函数在S3 Put上触发.
I want to extract the attachment from email and save it into my new S3 bucket. So far, I have configured AWS Simple Email Service to intercept incoming emails. Now I have an AWS lambda python function, which gets triggered on S3 Put.
在此之前,它一直在工作.但是我的lambda给出了错误提示:"[Errno 2]没有这样的文件或目录:'abc.docx':OSError".我看到S3的原始电子邮件中提到了名称为abc.docx的附件.
Until this it is working. But my lambda is giving error saying: "[Errno 2] No such file or directory: 'abc.docx': OSError". I see that the attachment with the name abc.docx is mentioned in the raw email in S3.
我认为问题出在我的upload_file中.你能在这里帮我吗?
I assume the problem is in my upload_file. Could you please help me here.
请在下面找到我代码的相关部分.
Please find below the relevant parts of my code.
s3 = boto3.client('s3')
s3resource = boto3.resource('s3')
waiterFlg = s3.get_waiter('object_exists')
waiterFlg.wait(Bucket=bucket, Key=key)
response = s3resource.Bucket(bucket).Object(key)
message = email.message_from_string(response.get()["Body"].read())
if len(message.get_payload()) == 2:
attachment = msg.get_payload()[1]
s3resource.meta.client.upload_file(attachment.get_filename(), outputBucket, attachment.get_filename())
else:
print("Could not see file/attachment.")
推荐答案
以下代码解决了该问题.
The following code solved the issue.
open('/tmp/newFile.docx', 'wb').write(attachment.get_payload(decode = True)) s3r.meta.client.upload_file('/tmp/newFile.docx',outputBucket,attachment.get_filename())
open('/tmp/newFile.docx', 'wb').write(attachment.get_payload(decode=True)) s3r.meta.client.upload_file('/tmp/newFile.docx', outputBucket, attachment.get_filename())
这篇关于从电子邮件中提取附件并将其保存(通过SES)到AWS S3中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!