Azure函数:System.InvalidOperationException:存储帐户连接字符串“”不存在

2024-04-28 17:40:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一个azure函数,目前azure函数充当Webhook消费者。该函数的任务是读取Webhook事件并保存到azure存储中。 我正在使用HTTP触发器模板来完成这项工作。我能够从Webhook接收事件,但是当我尝试写入azure存储时,它给了我以下错误

我尝试了这个post中提到的选项,但是仍然没有得到相同的错误

System.InvalidOperationException: Storage account connection string 'AzureWebJobs<AzureStorageAccountName>' does not exist. Make sure that it is a defined App Setting.

下面是我的function.json文件

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "blob",
      "name": "outputblob",
      "path": "test/demo",
      "direction": "out",
      "connection": "<AzureStorageAccountName>"
    }    
  ]
}

init.py

import logging

import azure.functions as func

def main(req: func.HttpRequest,outputblob: func.Out[str]) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = 'some_name'
    if not name:
        try:
            req_body = 'req_body_test'#req.get_json()
        except ValueError:
            pass
    else:
        name = 'name'#req_body.get('name')
    
    print(str(req.get_json()))

    outputblob.set(str(req.get_json()))

Tags: 函数namejsonhttpget错误事件body
1条回答
网友
1楼 · 发布于 2024-04-28 17:40:41

enter image description here

请确保已将连接字符串添加到本地的local.settings.json或azure的配置设置中

请测试以下代码和设置文件:

__init__.py

import logging

import azure.functions as func


def main(req: func.HttpRequest,outputblob: func.Out[func.InputStream]) -> func.HttpResponse:
    outputblob.set("this is a test.")
    return func.HttpResponse(
            "Test.",
            status_code=200
    )

function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
    {
      "name": "outputblob",
      "type": "blob",
      "path": "test/demo",
      "connection": "MyStorageConnectionAppSetting",
      "direction": "out"
    }
  ]
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "MyStorageConnectionAppSetting":"DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx;EndpointSuffix=core.windows.net"
  }
}

在azure上:

enter image description here

相关问题 更多 >