Python 在AWS SES中从content type=application/zip和base64编码解析文本文件
在amazon SES上,我有一个规则将传入的电子邮件保存到S3存储桶。Amazon以MIME格式保存这些文件 这些电子邮件的附件中有一个Python 在AWS SES中从content type=application/zip和base64编码解析文本文件,python,amazon-web-services,amazon-s3,zip,aws-lambda,Python,Amazon Web Services,Amazon S3,Zip,Aws Lambda,在amazon SES上,我有一个规则将传入的电子邮件保存到S3存储桶。Amazon以MIME格式保存这些文件 这些电子邮件的附件中有一个.txt,它将在MIME文件中显示为content type=text/plain,content Disposition=attachment。txt,和内容传输编码=引用的可打印或bases64 我能够使用python很好地解析它 当压缩.txt文件附件时(即内容类型:application/zip),我在解码该附件的内容时遇到问题,好像编码不是base6
.txt
,它将在MIME文件中显示为content type=text/plain
,content Disposition=attachment。txt
,和内容传输编码=引用的可打印或bases64
我能够使用python很好地解析它
当压缩.txt
文件附件时(即内容类型:application/zip
),我在解码该附件的内容时遇到问题,好像编码不是base64
我的代码:
import base64
s = unicode(base64.b64decode(attachment_content), "utf-8")
抛出错误:
Traceback (most recent call last):
File "<input>", line 796, in <module>
UnicodeDecodeError: 'utf8' codec can't decode byte 0xcf in position 10: invalid continuation byte
我也尝试过使用“latin-1”,但会胡言乱语。问题是,转换后,我处理的是一个压缩文件的格式,如“PK\x03\x04\X3C\Xa\x0c…
”,在转换为UTF-8 unicode之前,我需要将其解压缩
这个代码对我有用:
import email
# Parse results from email
received_email = email.message_from_string(email_text)
for part in received_email.walk():
c_type = part.get_content_type()
c_enco = part.get('Content-Transfer-Encoding')
attachment_content = part.get_payload()
if c_enco == 'base64':
import base64
decoded_file = base64.b64decode(attachment_content)
print("File decoded from base64")
if c_type == "application/zip":
from cStringIO import StringIO
import zipfile
zfp = zipfile.ZipFile(StringIO(decoded_file), "r")
unzipped_list = zfp.open(zfp.namelist()[0]).readlines()
decoded_file = "".join(unzipped_list)
print('And un-zipped')
result = unicode(decoded_file, "utf-8")
import email
# Parse results from email
received_email = email.message_from_string(email_text)
for part in received_email.walk():
c_type = part.get_content_type()
c_enco = part.get('Content-Transfer-Encoding')
attachment_content = part.get_payload()
if c_enco == 'base64':
import base64
decoded_file = base64.b64decode(attachment_content)
print("File decoded from base64")
if c_type == "application/zip":
from cStringIO import StringIO
import zipfile
zfp = zipfile.ZipFile(StringIO(decoded_file), "r")
unzipped_list = zfp.open(zfp.namelist()[0]).readlines()
decoded_file = "".join(unzipped_list)
print('And un-zipped')
result = unicode(decoded_file, "utf-8")