首页 > 解决方案 > 尝试使用 python 从 azure 函数触发 url,错误地说“没有 $return 绑定返回非无值”

问题描述

下面是代码,

import logging
import json 
import urllib.request
import urllib.parse
import azure.functions as func


def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")
    response = urllib.request.urlopen("http://example.com:5000/processing")
    return {
        'statusCode': 200,
        'body': json.dumps(response.read().decode('utf-8'))
    }

错误:结果:失败异常:RuntimeError:没有 $return 绑定的函数“abc”返回非无值堆栈:文件“/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py ",第 341 行,在 _handle__invocation_request f'function {fi.name!r} 中没有 $return 绑定'。相同的代码在 lambda 中有效。请帮助我调试 azure 函数。

函数.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "sourcemetadata/{name}",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

标签: pythonpython-3.xpython-2.7azure

解决方案


在 Azure 函数中,如果return在函数应用代码中使用,则表示要使用输出绑定。但是您没有在 function.json 中定义它。请定义它。有关更多详细信息,请参阅此处此处

例如

我使用带有 blob 触发器的进程 blob 并将消息发送到带有队列输出绑定的 azure 队列

  1. 函数.json
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "test/{name}.csv",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "$return",
      "direction": "out",
      "type": "queue",
      "queueName": "outqueue",
      "connection": "AzureWebJobsStorage"
    }
  ]
}
  1. 代码
async def main(myblob: func.InputStream) :
    
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n")
    return "OK"

在此处输入图像描述 在此处输入图像描述


推荐阅读