从S3下载一个图像文件,并使用BytesIO和MIMEApplication结果在python中以电子邮件形式发送到零字



我有这个源代码可以从S3存储桶下载文件其中我使用BytesIO将读取的数据存储在内存中,而不是文件中。

with io.BytesIO() as img:
img.name = 'screenshot.png'
try:
get_bucket().download_fileobj(key, img)
except Exception as e:
logger.error('ERROR processing screenshot, exception: %s',
str(e))
raise NotFoundError()

并通过这个发送电子邮件

part = MIMEApplication(attachment.read())
part.add_header('Content-Disposition', 'attachment',
filename=os.path.basename(attachment.name))
msg.attach(part)
_ses.send_raw_email(Source=sender, Destinations=recipients,
RawMessage={'Data': msg.as_string()})

其中attachments是img(BytesIO(对象。我的问题是,当收到电子邮件时,字节数为0,或者文件不可见。

你们知道这里发生了什么吗?或者有任何关于在哪里寻找这个问题的提示吗?

顺便说一下,该应用程序使用的是非常旧的boto31.13.13版本。这是一个旧系统,我们只是在做维护。

尝试在download_fileobj之后添加一个img.seek(0)

最新更新