为了账号安全,请及时绑定邮箱和手机立即绑定

Azure 函数“本地”文件

Azure 函数“本地”文件

红糖糍粑 2023-06-27 17:51:14
我正在 Python 中创建一个 HTTP 函数,它将从存储中获取 csv blob,使用 scipy 和 matplotlib 执行一些操作,并使用 wkhtmltopdf 将 pdf 输出到同一容器。我在本地系统上执行任何此操作都没有问题,但像大多数事情一样,当我部署它时它会停止工作。该函数需要覆盖 pickle 文件(在 blob 存储中),创建多个图像 (.png) 和 html 文件,并引用 wkhtmltopdf 的 exe 将这些 png 和 html 转换为 pdf。pickle 部分首先出现,并在此行上捕获此错误所以看起来我无法写入当前目录,这将证明是图像的问题。这是处理这种情况的错误方法吗?有人可以问我正确的问题来缩小问题的根源吗?下面是我的代码的一个子集:sas_token_output_html = generate_blob_sas(account_name='***',                            account_key='***',                            container_name=container,                            blob_name='output.html',                            permission=BlobSasPermissions(read=True, write=True, create=True),                            expiry=datetime.datetime.utcnow() + datetime.timedelta(hours=1))output_html_url =f'https://***.blob.core.windows.net/{container}/output.html?{sas_token_output_html}'template_vars = {        'week_day':'Wednesday',        'month':datetime.date.today().strftime('%B'),        'day': datetime.date.today().day,        'year': datetime.date.today().year,        ...}        message_html = template.render(template_vars)with open('output_html_url','w') as f:    f.write(message_html)
查看完整描述

2 回答

?
富国沪深

TA贡献1790条经验 获得超9个赞

我无法在本地存储任何内容,但我能够将我的帮助程序文件/函数存储在存储 blob 中,并使用以下命令在本地下载 blob:

with open(os.path.join(tempfile.gettempdir(), 'input.csv'), 'wb') as input:
        input.write(blob_client.download_blob().readall())

这并不能解决具体问题,但它是一个可以接受的解决方法。


查看完整回答
反对 回复 2023-06-27
?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

我看到这个片段希望能完成类似的事情。我把与我无关的部分都拿出来了。这可以在 vscode 中使用函数应用程序的测试功能时创建一个文件(func start):(来自https://option40.com/blog/azure)


import logging

import tempfile

import azure.functions as func



def main(req: func.HttpRequest) -> func.HttpResponse:

    logging.info('Python HTTP trigger function processed a request.')


    dir_path = tempfile.gettempdir()

    file = dir_path + "\test.txt"

    name = "Me"


    with open(file, mode="w") as f:

        f.write(f"{name}")

        

    with open(file) as f:

        new = f.read()


    if name:

        return func.HttpResponse(f"{file}, {new}")

    else:

        return func.HttpResponse(

             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",

             status_code=200

        )

我的结果:C:\Users\myName\AppData\Local\Temp\test.txt,Me


查看完整回答
反对 回复 2023-06-27
  • 2 回答
  • 0 关注
  • 138 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信