python - 是否可以在 Python 中的 Azure Function Linux 消费计划中保存临时文件?

标签 python file permissions save azure-functions

首先对不起我的英语。我有一个使用 Python 的 Azure Function Linux 消费计划,我需要生成一个 html,使用 wkhtmltopdf 转换为 pdf 并通过电子邮件发送。

 #generate temporally pdf
config = pdfkit.configuration(wkhtmltopdf="binary/wkhtmltopdf")
pdfkit.from_string(pdf_content, 'report.pdf',configuration=config, options={})

#read pdf and transform to Bytes
with open('report.pdf', 'rb') as f:
    data = f.read()

#encode bytes
encoded = base64.b64encode(data).decode()

#Send Email
EmailSendData.sendEmail(html_content,encoded,spanish_month)
代码在我的本地开发中运行正常,但是当我部署函数并执行代码时,我收到一条错误消息:
Result: Failure Exception: OSError: wkhtmltopdf reported an error: Loading pages (1/6) [> ] 0% [======> ] 10% [==============================> ] 50% [============================================================] 100% QPainter::begin(): Returned false Error: Unable to write to destination
我认为报告错误是因为由于任何原因写权限不可用。你能帮我解决这个问题吗?
提前致谢。

最佳答案

tempfile.gettempdir()方法返回一个临时文件夹,在 Linux 上是 /tmp .您的申请可以使用此目录存储临时文件 由您的函数在执行期间生成和使用。
所以使用 /tmp/report.pdf作为保存临时文件的文件目录。

with open('/tmp/report.pdf', 'rb') as f:
    data = f.read()
更多详情可以引用这个article .

关于python - 是否可以在 Python 中的 Azure Function Linux 消费计划中保存临时文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63265669/

相关文章:

python - 有没有一种方法可以在不使用 Python 中的 eval 的情况下完成 eval 的功能

python - Django-haystack:添加自动完成所需的 `content_auto` 行后,rebuild_index 失败(haystack.exceptions.SearchFieldError)

mysql - 如何授予除一列以外的所有列的权限?

permissions - virtualenv python 可执行文件 (lubuntu-12.10) 的奇怪权限问题

postgresql - postgresql中创建表分区的权限继承

python - 导入的第三方模块没有 __file__ 属性

python - 使用 django-filter 进行不区分大小写的排序

python - 搜索 Python 列表

python - 如何将每 12 行写入一个新文件

java - 从 jar 中读取资源文件