最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Azure Function http_trigger reuse Cosmos DB client - Stack Overflow

programmeradmin3浏览0评论

I am creating an Azure function in Python with a http_trigger. It’s using the v2 model:

import azure.functions as func
import logging

app = func.FunctionApp()

@app.route(route="customers/{id:guid?}", auth_level=func.AuthLevel.FUNCTION, methods=["GET", "POST", "PUT", "DELETE"])
def customers(req: func.HttpRequest) -> func.HttpResponse:

If I create a singleton Cosmos DB client as mentioned in Manage connections in Azure Functions and Performance tips for Azure Cosmos DB Python SDK in the global area as follows:

import azure.functions as func
import logging
from azure.cosmos import CosmosClient
from azure.identity import DefaultAzureCredential
import os

cosmos_account = os.environ["COSMOS_ACCOUNT"]
cosmos_client = CosmosClient(f"https://{cosmos_account}.documents.azure:443/", credential)
cosmos_database = cosmos_client.get_database_client(os.environ["COSMOS_DATABASE"])
cosmos_container = cosmos_database.get_container_client(os.environ["COSMOS_CONTAINER"])

app = func.FunctionApp()

@app.route(route="customers/{id:guid?}", auth_level=func.AuthLevel.FUNCTION, methods=["GET", "POST", "PUT", "DELETE"])
def customers(req: func.HttpRequest) -> func.HttpResponse:

The function "customers" fails to be detected when it's loaded into my Azure Function. However if I remove that Cosmos DB client creation from the global space and move it into the "customers" function, it is loaded and able to be executed.

Based on best practices from the above links, it appears it should be a global client that can be reused. Can anyone share the proper syntax?

发布评论

评论列表(0)

  1. 暂无评论