使用带有 Python 的 Azure Function App 更新 Azure Table

Update Azure Table using Azure Function App with Python

我正在尝试从我的 Azure 函数更新 Azure Table 中的现有行,但出现以下错误:

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 to indicate that you need to specify an ETag : '*', but I have been unsuccessful with this (I'm probably not using it correctly). There is a (从引用的 git 问题链接)。一些进一步的研究似乎表明 ETag 值需要成为 header 的一部分,但我无法证实这一点,如果它是真的,我是否看到 where/how 我可以通过 headers.

下面我使用 '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}.")

绑定

{
  "type": "table",
  "direction": "out",
  "name": "functionTableStorage",
  "tableName": "masterTable",
  "connection": "AzureWebJobsStorage"
},

由于要使用 Function App 更新 Azure Table,因此不应使用输出绑定。

如果我对你想要的理解正确,你应该将基本逻辑放在函数触发器的主体中,如下所示:(在调试之前,你应该首先 运行 这个命令: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
        )

这是我原来的实体:

这是更新后的实体:

这是官方文档:

https://docs.microsoft.com/en-us/azure/cosmos-db/table-storage-how-to-use-python?toc=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fstorage%2Ftables%2Ftoc.json&bc=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fbread%2Ftoc.json#update-an-entity