我想从电子邮件中提取附件并将其保存到我的新S3存储桶中。到目前为止,我已经配置了AWS Simple Email Service来拦截传入的电子邮件。现在我有一个AWS lambda python函数,它在S3 Put上触发。通过电子邮件(通过SES)提取并保存附件到AWS S3
直到它工作。但是我的lambda错误地说:“[Errno 2]没有这样的文件或目录:'abc.docx':OSError”。我看到在S3的原始电子邮件中提到了名称为abc.docx的附件。
我假设问题出在我的upload_file中。你能帮我在这里吗?
请在下面找到我的代码的相关部分。
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.")
您可以尝试将附件下载到Lambda中的/ tmp目录,然后上载到S3。 – Ashan
事实上,我正在尝试,现在似乎工作。非常感谢您的帮助。下面的代码帮助解决了这个问题。('/ tmp/newFile.docx','wb')。write(attachment.get_payload(decode = True)) s3r.meta.client.upload_file('/ tmp/newFile。 docx',outputBucket,attachment.get_filename()) – user3567195