python - 将输出写入 Azure Functions 中的 Blob

标签 python azure azure-functions azure-blob-storage

我正在学习使用 Azure 函数。所以,我可能听起来很愚蠢。我正在编写一个计时器触发函数,该函数每 1 分钟运行一次,添加两个数字并写入文件。这工作正常,因为我可以将输出写入本地服务器上的文件。

第二步,我想将输出写入 blob。下面是代码:

import datetime
import logging  
import azure.functions as func

a=4
b=5
sum=a+b
file_name= open("sum.txt","w+")


def main(mytimer: func.TimerRequest, outputBlob: func.Out[str]) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()
    if mytimer.past_due:
        global sum
        global file_name
        print("sum:", sum)
        logging.info('The sum has been calculated!')



    logging.info(sum)
    logging.info('Python timer trigger function ran at %s', utc_timestamp)
    with open("sum.txt", "a") as file_name:
        file_name.seek(0)
        file_name.write("\n")
        file_name.write("Sum: %s" % sum)

    outputBlob.set(file_name)

但是,当我运行该函数时,出现以下错误:

[10/02/2020 14:06:00] Executed 'Functions.CalcPayment' (Failed, Id=547f7a3d-03b4-4a02-98e7-f4bfb73e6f5e)
[10/02/2020 14:06:00] System.Private.CoreLib: Exception while executing function: Functions.CalcPayment. System.Private.CoreLib: Result: Failure
[10/02/2020 14:06:00] Exception: TypeError: unable to encode outgoing TypedData: unsupported type "<class 'azure_functions_worker.bindings.generic.GenericBinding'>" for Python type "int"
[10/02/2020 14:06:00] Stack:   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.2106/workers/python/3.7/OSX/X64/azure_functions_worker/dispatcher.py", line 330, in _handle__invocation_request
[10/02/2020 14:06:00]     pytype=out_type_info.pytype)
[10/02/2020 14:06:00]   File "/usr/local/Cellar/azure-functions-core-tools@3/3.0.2106/workers/python/3.7/OSX/X64/azure_functions_worker/bindings/meta.py", line 83, in to_outgoing_proto
[10/02/2020 14:06:00]     f'unable to encode outgoing TypedData: '
[10/02/2020 14:06:00] .

最佳答案

您不需要写入文件,只需创建一个适合您的情况的字符串并将其推送到输出变量即可。您可以使用this样本作为起点。

关于python - 将输出写入 Azure Functions 中的 Blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60152416/

相关文章:

c# - 从另一个函数调用 HTTP 触发的 Azure 函数

node.js - Azure函数JS : get image from blob and transform to base64

azure - 如何在 Azure Functions 中使用多种身份验证方案

python - 嵌套 for 循环到 numpy 卷积

css - 从 Azure CDN 请求的字体文件被 CORS 策略阻止

iis - 确定 AZURE 上的 W3SCV 名称

linux - 部署Azure Function的gitlab代码-如何控制版本号

python - 用Python计算IRR

python - bool 值 `loc` 和后续 `iloc` 的 Pandas 索引

python - 你如何在 TensorFlow 中将渐变固定到某个 GPU?