我有一个斑点触发功能。此代码片段来自 init.py:
credential = DefaultAzureCredential()
endpoint = os.environ["connect__accountEndpoint"]
client = CosmosClient(
url = endpoint,
credential = credential
)
database_name = os.environ["cosmosDatabase"]
container_name = os.environ["cosmosColl"]
database = client.get_database_client(database_name)
container = database.get_container_client(container_name)
这是我的 local.settings.json:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsStorage": "",
"AzureWebJobsSecretStorageType": "files",
"cosmosDatabase": "",
"cosmosColl": "",
"connect__accountEndpoint": "",
"connect__clientId": "",
"BlobConnStr": ""
}
}
这是我的 function.json :
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "myblob",
"type": "blobTrigger",
"direction": "in",
"path": "contracts/{name}",
"connection": "BlobConnStr"
},
{
"direction": "out",
"type": "cosmosDB",
"name": "doc",
"databaseName": "%cosmosDatabase%",
"collectionName": "%cosmosColl%",
"connect__accountEndpoint": "connect__accountEndpoint",
"createIfNotExists": true
}
]
}
我收到此错误:
System.Private.CoreLib: Exception while executing function: Functions.contracts_blob_trigger. Microsoft.Azure.WebJobs.Extensions.CosmosDB: Cosmos DB connection configuration 'CosmosDB' does not exist. Make sure that it is a defined App Setting.
我在 json 文件之间的配置部分遇到问题。我该如何处理这个错误?
第一期是
connect__accountEndpoint
。您在 Azure Functions 中使用特殊符号,通常意味着存在嵌套 JSON:
{
"connect":
{
"accountEndpoint":"...."
}
}
在
Values
部分。但是您的 Values
部分没有具有 connection
和 accountEndpoint
属性的 clientId
节点。
但是看看您正在使用的内容,您似乎正在尝试使用 MSI 身份验证(accountEndpoint + clientId),这与您似乎正在使用的扩展版本不兼容。
Extension and Bundles 4.x 支持 MSI Auth,其属性和配置的名称不同(
collectionName
不再存在,它的 containerName
)。
请确保您使用的是最新的捆绑包(https://learn.microsoft.com/azure/azure-functions/migrate-cosmos-db-version-3-version-4?tabs=isolated-process&pivots=programming- language-python),然后你的
function.json
看起来像:
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "myblob",
"type": "blobTrigger",
"direction": "in",
"path": "contracts/{name}",
"connection": "BlobConnStr"
},
{
"direction": "out",
"type": "cosmosDB",
"name": "doc",
"databaseName": "%cosmosDatabase%",
"containerName": "%cosmosColl%",
"connection": "connect",
"createIfNotExists": true
}
]
}
还有你的
host.json
:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsStorage": "",
"AzureWebJobsSecretStorageType": "files",
"cosmosDatabase": "",
"cosmosColl": "",
"connect": {
"accountEndpoint":"",
"clientId":""
},
"BlobConnStr": ""
}
}