使用 Python 的 Azure 函数 - Blob 触发器未触发

时间:2021-01-13 08:21:12

标签: python azure azure-functions azure-storage-blobs azure-function-app

我正在研究一个用 Python 编写的 Azure 函数,它应该基于 Blob 触发事件被调用。但是,当我在 azure 函数应该监视的 blob 容器中上传 zip 文件时,触发器不会触发。

以下是local.settings.json文件-

{
  "IsEncrypted": false,
  "Values": 
    {
    "AzureWebJobsStorage": "blob (connection string) that was created when Azure function is created",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "My_STORAGE": "blob (connection string) that function should monitor"
    }
} 

以下是function.json文件-

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

以下是我的代码 init.py - (test_func 是用户定义的函数,用于执行一些业务逻辑)

def main(myblob: func.InputStream):
test_func(myblob.name)
logging.info(f"Python blob trigger function processed blob \n"
             f"Name: {myblob.name}\n"
             f"Blob Size: {myblob.length} bytes")

当我在“mycontainer”容器中上传 zip 文件时,azure 函数没有触发。

StorageV2(通用 v2) 帐户类型的“mycontainer”。我使用的是 Python 3.8 版本。

这个“mycontainer”已经自动创建了一个名为 $logs 的容器,它有一个 day wise 文件夹来有一个日志文件提到我在“mycontainer”中上传的文件,但是,Azure 上没有 blob 触发事件的迹象功能。

“My_STORAGE”被添加为 Azure Function 的配置设置中的应用程序设置。我正在 Azure Function 部署后上传本地设置

知道出了什么问题吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

问题是由连接字符串错误引起的。连接字符串中的 AccountName 应该是存储帐户名称,而不是容器名称。

所以只需将 AccountName=mycontainer 更改为 AccountName=<storage account name> 即可。

顺便说一句:

连接字符串应该是:DefaultEndpointsProtocol=https;AccountName=<storage account name>;AccountKey=xxxxxxxxxx==;EndpointSuffix=core.windows.net

“function.json”中的“路径”应该是:"path": "<container name>/{name}"

答案 1 :(得分:1)

我也遇到过类似的问题。通过将连接字符串作为键值对添加到Function App->Configuration->Settings中的Application Settings中解决了这个问题。我假设如果 Azure Functions Apps 部署在不同的存储容器中,则必须这样做,为此需要有不同的连接字符串。

相关问题