简体   繁体   English

将输出写入 Azure Functions 中的 Blob

[英]Write output to Blob in Azure Functions

I am learning to use Azure functions.我正在学习使用 Azure 函数。 So, I might sound stupid.所以,我可能听起来很愚蠢。 I am writing a timer trigger function which runs every 1 minute and adds two number and writes to a file.我正在编写一个定时器触发器函数,它每 1 分钟运行一次,并添加两个数字并写入一个文件。 This works fine since I can write the output to the file on the local server.这工作正常,因为我可以将输出写入本地服务器上的文件。

As a second step, I wanted to write the output to the blob.第二步,我想将输出写入 blob。 Below is the code:下面是代码:

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)

However, when I run the function I get the below error:但是,当我运行该函数时,出现以下错误:

[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] .

you dont need to have a file to write to, just create a string that makes sense in your case and push it to the output variable.您不需要有要写入的文件,只需创建一个对您的情况有意义的字符串并将其推送到输出变量即可。 You can use this sample as a starting point.您可以使用示例作为起点。

Ok.好的。 So the issue was very simple.所以问题很简单。 I had to convert the sum to string and write to the blob and everything worked.我必须将总和转换为字符串并写入 blob,一切正常。 Thanks !!谢谢 !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM