python - 使用 Azure Function App 和 Python 更新 Azure 表

标签 python azure azure-functions azure-table-storage

我尝试从我的 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
        )

这是我原来的实体:

enter image description here

这是更新后的实体:

enter image description here

这是官方文档:

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

关于python - 使用 Azure Function App 和 Python 更新 Azure 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62780962/

相关文章:

azure - 尝试获取 Power BI 事件事件(审核日志)时出现 401 错误

azure - 升级到 ASP.NET 5 beta5 后如何进一步调试 500 内部服务器错误

azure - 在 azure 函数绑定(bind)中使用 http header 值

c# - 无法使用 Azure Functions 加载文件或程序集 System.Fabric

python - ExcelWriter 在书写时覆盖工作表

Python Goose 无法提取 mashable/usatoday/politicalwire 文章

azure - 连续执行Azure Function

azure - 无法创建集合信息 - 尝试在本地测试 Cosmos DB 触发器时出错

python - 如何检测脚本是从 Django 还是命令提示符运行?

python - 用于 Python 的 IDE 是什么?