Python V2版Azure函数访问Blob时触发NoneType错误求助
问题描述
在Windows系统上通过VSCode搭建Python V2版Azure函数,编写定时器触发的Blob输入输出测试代码后,运行时持续触发TypeError: object of type 'NoneType' has no len()错误,报错指向代码中len(inputblob)行。已升级Python至3.11,尝试添加data_type参数、使用InputStream并调用read()方法,问题仍未解决。
测试代码
@app.timer_trigger(schedule="*/5 * * * * *", arg_name="myTimer", run_on_startup=True, use_monitor=False) @app.blob_input(arg_name="inputblob", path="samples/test.txt", connection="AzureWebJobsStorage") @app.blob_output(arg_name="outputblob", path="newblob/test.txt", connection="AzureWebJobsStorage") #def TimerTrigger(myTimer: func.TimerRequest, inputblob: str, outputblob: func.Out[str]) -> None: def TimerTrigger(myTimer, inputblob, outputblob) : if myTimer.past_due: logging.info('The timer is past due!') logging.info('Python timer trigger function executed.') logging.info(f'Python Queue trigger function processed {len(inputblob)} bytes') outputblob.set(inputblob) return "ok"
报错信息
Executed 'Functions.TimerTrigger' (Failed, Id=b1ab178c-e66a-4bbe-9ee6-bc9dc9241120, Duration=15ms)
[2023-08-15T19:59:55.023Z] System.Private.CoreLib: Exception while executing function: Functions.TimerTrigger. System.Private.CoreLib: Result: Failure
Exception: TypeError: object of type 'NoneType' has no len()
Stack: File "C:\Users\T8\AppData\Roaming\nvm\v16.18.0\node_modules\azure-functions-core-tools\bin\workers\python\3.11\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 479, in _handle__invocation_request
call_result = await self._loop.run_in_executor(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\T8\AppData\Local\Programs\Python\Python311\Lib\concurrent\futures\thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\T8\AppData\Roaming\nvm\v16.18.0\node_modules\azure-functions-core-tools\bin\workers\python\3.11\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 752, in _run_sync_func
return ExtensionManager.get_sync_invocation_wrapper(context,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\T8\AppData\Roaming\nvm\v16.18.0\node_modules\azure-functions-core-tools\bin\workers\python\3.11\WINDOWS\X64\azure_functions_worker\extension.py", line 215, in _raw_invocation_wrapper
result = function(**args)
^^^^^^^^^^^^^^^^
File "C:\Users\T8\source\repos\Q8Utilities\filefuncapp\function_app.py", line 50, in TimerTrigger
logging.info(f'Python Queue trigger function processed {len(inputblob)} bytes')
解决方案
检查Blob路径与文件存在性:
确认Azure存储账户中,指定路径samples/test.txt对应的Blob文件存在。注意Azure Blob的路径格式为容器名/Blob文件名(虚拟目录为容器名/虚拟目录/Blob文件名),且文件名区分大小写,需与代码中路径完全一致。验证连接字符串配置:
检查local.settings.json中的AzureWebJobsStorage连接字符串是否正确指向目标存储账户,格式如下:{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "你的存储账户连接字符串", "FUNCTIONS_WORKER_RUNTIME": "python" } }添加代码防御性判断:
在代码中增加inputblob空值判断,避免因Blob不存在导致函数崩溃,修改后的代码示例:def TimerTrigger(myTimer, inputblob, outputblob) : if myTimer.past_due: logging.info('The timer is past due!') logging.info('Python timer trigger function executed.') if inputblob is not None: logging.info(f'Python Blob trigger function processed {len(inputblob)} bytes') outputblob.set(inputblob) else: logging.warning('Input blob does not exist or cannot be accessed') return "ok"升级Azure Functions Core Tools:
旧版本工具可能存在Python V2绑定兼容性问题,执行以下命令升级至最新版:npm install -g azure-functions-core-tools@4
内容的提问来源于stack exchange,提问作者Tyrone Jones

