How to include non-python reference files in Azure Function?

*爱你&永不变心* 提交于 2021-02-10 18:20:39

问题


I have some Python code that references external .ttf font files. I can reference them by including them in the project root folder. I'd like to turn this code into an HTTP Triggered Azure Function.

I see in the docs that I can import external Python modules and files into a Function, but what about non-Python files?

How are these referenced?

  • When Python code is run locally, they are referenced in the code as:
h1_font = ImageFont.truetype("DejaVuSans-Bold.ttf", h1_size)
h2_font = ImageFont.truetype("Ubuntu-Th.ttf", h2_size)
h3_font = ImageFont.truetype("Ubuntu-Th.ttf", h3_size)
footer_font = ImageFont.truetype("UbuntuMono-R.ttf", footer_size)

What is the filepath when the app is published to Azure?

  • Here is how the folders are structured locally:

Once published to Azure, the .ttf files are no longer accessible and Function fails

Note: This is a Python Function running on Linux, not Windows.


回答1:


Update:

My structure:

__init__.py

import logging

import azure.functions as func
from fontTools.ttLib import TTFont


def main(req: func.HttpRequest) -> func.HttpResponse:
    font = TTFont('HttpTrigger1/font.ttf')
    with open('HttpTrigger1/test.txt', 'r') as file:
        data = file.read()
    return func.HttpResponse("The content is -----------------"+data)
    

Original Answer:

How are these referenced?

Put it in the Project folder and use relative path. Then use it as before.

What is the filepath?

Physical path of azure function app is D:\home\site\wwwroot.

Do I just include them in the root folder of the Function and publish?

Yes, just include them in the root folder and then publish the function app, and then you can use relative path to get them.



来源:https://stackoverflow.com/questions/65486646/how-to-include-non-python-reference-files-in-azure-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!