我尝试从我的 Azure Function 更新 Azure 表中的现有行,但出现错误:
Functions.HttpTrigger1. Microsoft.Azure.WebJobs.Host: Error while handling parameter _binder after function returned:. Microsoft.Azure.WebJobs.Extensions.Storage: The specified entity already exists.
一些research seems表明您需要指定 ETag : '*'
,但我没有成功(我可能没有正确使用它)。有一个C# sample here (从引用的 git 问题链接)。一些进一步的研究似乎表明 ETag
值需要成为 header 的一部分,但我无法确认这一点,也无法确认是否属实,我是否看到在哪里/如何传递 header 。
下面我使用“owner”作为 RowKey,希望在新触发器上更新“val2Update”。
Py代码
def main(req: func.HttpRequest, functionTableStorage: func.Out[str], messageJSON) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
owner = req.params.get('owner')
val2Update = req.params.get('val')
if owner:
data = {
"PartitionKey": "message",
"RowKey": owner,
"tester" : val2Update,
"ETag": "*"
}
functionTableStorage.set(json.dumps(data))
return func.HttpResponse(f"Thanks, {owner}.")
绑定(bind)
{
"type": "table",
"direction": "out",
"name": "functionTableStorage",
"tableName": "masterTable",
"connection": "AzureWebJobsStorage"
},
最佳答案
由于您想使用 Function App 更新 Azure 表,因此不应使用输出绑定(bind)。
如果我正确理解您想要的内容,您应该将基本逻辑放入函数触发器的主体中,如下所示:(在调试之前,您应该首先运行此命令:pip install azure-cosmosdb-table
)
import logging
import azure.functions as func
from azure.cosmosdb.table.tableservice import TableService
from azure.cosmosdb.table.models import Entity
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
table_service = TableService(connection_string='DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx;EndpointSuffix=core.windows.net')
task = {'PartitionKey': 'tasksSeattle', 'RowKey': '001',
'description': 'Take out the garbage', 'priority': 250}
table_service.update_entity('tasktable', task)
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:
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
)
这是我原来的实体:
这是更新后的实体:
这是官方文档:
关于python - 使用 Azure Function App 和 Python 更新 Azure 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62780962/