繁体   English   中英

使用 Azure Function 应用程序更新 Azure 表与 ZA7F5F35426B9237811FCZ2317B5

[英]Update Azure Table using Azure Function App with Python

我正在尝试从我的 Azure Function 更新 Azure 表中的现有行,但错误:

函数.HttpTrigger1。 Microsoft.Azure.WebJobs.Host:在 function 返回后处理参数 _binder 时出错:。 Microsoft.Azure.WebJobs.Extensions.Storage:指定的实体已经存在。

一些研究似乎表明您需要指定一个ETag: '*' ,但我没有成功(我可能没有正确使用它)。 这里有一个C# 示例(链接自参考的 git 问题)。 一些进一步的研究似乎表明ETag值需要是 header 的一部分,但我无法证实这一点,如果它是真的,我是否看到我可以在哪里/如何传递标头。

下面我使用“所有者”作为 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 应用程序更新 Azure 表,因此不应使用 output 绑定。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM