我可以部署 Azure Function,但它不会将数据添加到 SQL 数据库,该数据库具有与之关联的 Azure 托管标识。
function_app.py
import azure.functions as func
import logging
import uuid
from azure.functions.decorators.core import DataType
app = func.FunctionApp()
@app.function_name(name="HttpTrigger1")
@app.route(route="hello", auth_level=func.AuthLevel.ANONYMOUS)
@app.generic_output_binding(arg_name="toDoItems", type="sql", CommandText="dbo.ToDo", ConnectionStringSetting="Server=X.database.windows.net; Authentication=Active Directory Default; Database=X;", data_type=DataType.STRING)
def test_function(req: func.HttpRequest, toDoItems: func.Out[func.SqlRow]) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
toDoItems.set(func.SqlRow({"id": uuid.uuid4(), "title": name, "completed": "false", "url": "www"}))
return func.HttpResponse(f"Hello {name}!")
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body",
status_code=400
)
host.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
我已使用以下步骤进行 SQL 输出绑定以在 python V2 函数中工作,这里我使用 ADO.NET(Microsoft Entra 无密码身份验证) 通过在函数应用程序中启用托管身份来使用 SQL 凭据。
import azure.functions as func
import logging
from azure.functions.decorators.core import DataType
import uuid
app = func.FunctionApp()
@app.function_name(name="HttpTrigger1")
@app.route(route="hello", auth_level=func.AuthLevel.ANONYMOUS)
@app.generic_output_binding(arg_name="toDoItems", type="sql", CommandText="dbo.ToDo", ConnectionStringSetting="SqlConnectionString",
data_type=DataType.STRING)
def test_function(req: func.HttpRequest, toDoItems: func.Out[func.SqlRow]) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
item_id = str(uuid.uuid4()) # Convert UUID to string
toDoItems.set(func.SqlRow({"id": item_id, "title": name, "completed": False, "url": "www"}))
return func.HttpResponse(f"Hello {name}!")
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body",
status_code=400
)
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
"SqlConnectionString": "Server=tcp:{serverName}.database.windows.net,1433;Initial Catalog={DatabaseName};Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Authentication=Active Directory Default;"
}
}
CREATE USER [managed_identity_name] FROM EXTERNAL PROVIDER;
ALTER ROLE db_datareader ADD MEMBER [managed_identity_name];
ALTER ROLE db_datawriter ADD MEMBER [managed_identity_name];
ALTER ROLE db_ddladmin ADD MEMBER [managed_identity_name];
GO