簡體   English   中英

是否可以在 Azure Function Linux 消耗計划中保存臨時文件?

[英]Is possible to save a temporaly file in a Azure Function Linux Consuption Plan in Python?

首先對不起我的英語。 I have an Azure Function Linux Consuption Plan using Python and I need to generate an html, transform to pdf using wkhtmltopdf and send it by email.

 #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)

代碼在我的本地開發中運行正常,但是當我部署 function 並執行代碼時,我收到一條錯誤消息:

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()

更多細節,你可以參考這篇文章

最終正確代碼:

config = pdfkit.configuration(wkhtmltopdf="binary/wkhtmltopdf")

local_path = os.path.join(tempfile.gettempdir(), 'report.pdf')
logger.info(tempfile.gettempdir())

pdfkit.from_string(pdf_content, local_path,configuration=config, options={})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM