我正在使用 Azure 函数来处理存储在 Azure 存储表中的数据。 Azure Functions 是用 PowerShell 编写的,由计时器触发,每分钟运行一次。在每次运行时,它将获取前 10 个未处理的行,处理其中的每一行,然后通过将字段“已处理”设置为 true 来将行标记为已处理。但是,似乎通过使用输出绑定我无法更新一行。
我尝试使用以下配置和代码更新行。
函数绑定:
{
"tableName": "tobeprocessed",
"connection": "ProcessStorage",
"name": "InProcessStorageTableBinding",
"type": "table",
"direction": "in"
},
{
"tableName": "tobeprocessed",
"connection": "ProcessStorage",
"name": "OutProcessStorageTableBinding",
"type": "table",
"direction": "out"
}
功能码:
param($Timer, $InProcessStorageTableBinding)
# Get first ten unprocessed rows
$InProcessStorageTableBinding | Where-Object { $_.Processed -eq $false } | Select-Object -First 10 | ForEach-Object {
# Logic for processing the values in the row
# left out for the sake of brevity
# Update the row
$_.Processed = $true
Push-OutputBinding -Name OutProcessStorageTableBinding -Value $_
}
Azure 门户上 Azure Functions 的日志中出现的消息是:
指定的实体已经存在。 RequestId:da4224dd-0002-0071-0671-70a732000000 时间:2023-04-16T14:38:05.4671520Z
在互联网上搜索可能的解决方案时,我发现人们正在使用 PowerShell 模块
Get-AzStorageTable
中的Update-AzTableRow
和AzTable
来更新 Azure 存储表中的行。但是这样做需要更多的配置(证书、租户和应用程序 ID 等),因为似乎通过使用 Update-AzTableRow
我们不能使用 out 绑定。绑定是 Azure Functions 的一个很好的功能,因为它负责幕后的所有身份验证和连接设置,所以如果可能的话,我真的更喜欢使用 out 绑定。
更新:正如 Peter Bons 在下面的答案中指出的那样,关于 Azure 表输出绑定 的文档非常清楚,使用绑定无法进行更新。
然后是否可以重新使用(部分)由绑定设置的连接以减少所需的管道以便能够使用(例如)PowerShell 模块
AzTable
更新行
这样我们就可以做这样的事情:
$table = Get-AzStorageTable –Connection $InProcessStorageTableBinding.Connection
$cloudTable = $table.CloudTable
$rowToProcess = Get-AzTableRow `
-table $cloudTable `
-customFilter <filter to retrieve row>
$rowToProcess.Processed = $true
$rowToProcess | Update-AzTableRow -table $cloudTable
恐怕你运气不好。 文档证实了你的怀疑:
此输出绑定仅支持在表中创建新实体。如果您需要从函数代码更新现有实体,请直接使用 Azure Tables SDK。