英文:
How to update a row in an Azure storage table using an Azure Function out binding
问题
I'm using an Azure Function to process data stored in an Azure Storage table. The Azure Function is written in PowerShell and is triggered by a timer to run each minute. On each run, it will take the first 10 unprocessed rows, process each of those rows, and then mark the row as processed by setting a field 'processed' to true. However, it seems that by using the output binding I'm not able to update a row.
I tried updating the rows by using the following configuration and code.
Function bindings:
{
"tableName": "tobeprocessed",
"connection": "ProcessStorage",
"name": "InProcessStorageTableBinding",
"type": "table",
"direction": "in"
},
{
"tableName": "tobeprocessed",
"connection": "ProcessStorage",
"name": "OutProcessStorageTableBinding",
"type": "table",
"direction": "out"
}
Function code:
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 $_
}
The message that appears in the logs of the Azure Function on the Azure Portal is:
The specified entity already exists. RequestId: da4224dd-0002-0071-0671-70a732000000 Time: 2023-04-16T14:38:05.4671520Z
While searching the internet for possible solutions, I found that people are using Get-AzStorageTable
and Update-AzTableRow
from the PowerShell module AzTable
to update rows in Azure Storage tables. But by doing so, a lot more configuration is required (certificates, tenant- and app id's, etc) as it seems that by using Update-AzTableRow
we cannot use the out binding. And the bindings are such a nice feature of Azure Functions since it takes care of all the authentication and connection setup behind the scenes, so I would really prefer to use the out binding if possible.
Update: As Peter Bons pointed out in the answers below, the docs about Azure Tables output bindings are very clear that updating isn't possible using the bindings.
Would it then maybe be possible to re-use (parts) of the connection that is set up by the bindings to reduce the plumbing needed in order to be able to use (for instance) the PowerShell module AzTable
to update a row?
So that we could do something like this:
$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
英文:
I'm using an Azure Function to process data stored in an Azure Storage table. The Azure Function is written in PowerShell and is triggered by a timer to run each minute. On each run it will take the first 10 unprocessed rows, process each of those rows and then mark the row as processed by setting a field 'processed' to true. However, it seems that by using the output binding I'm not able to update a row.
I tried updating the rows by using the following configuration and code.
Function bindings:
{
"tableName": "tobeprocessed",
"connection": "ProcessStorage",
"name": "InProcessStorageTableBinding",
"type": "table",
"direction": "in"
},
{
"tableName": "tobeprocessed",
"connection": "ProcessStorage",
"name": "OutProcessStorageTableBinding",
"type": "table",
"direction": "out"
}
Function code:
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 $_
}
The message that appears in the logs of the Azure Function on the Azure Portal is:
> The specified entity already exists. RequestId:da4224dd-0002-0071-0671-70a732000000 Time:2023-04-16T14:38:05.4671520Z
While searching the internet for possible solutions I found that people are using Get-AzStorageTable
and Update-AzTableRow
from the PowerShell module AzTable
to update rows in Azure Storage tables. But by doing so a lot more configuration is required (certificates, tenant- and app id's, etc) as it seems that by using Update-AzTableRow
we cannot use the out binding. And the bindings is such a nice feature of Azure Functions since it takes care of all the authentication and connection setup behind the scenes, so I would really prefer to use the out binding if possible.
Update: As Peter Bons pointed out in the answers below, the docs about Azure Tables output bindings are very clear that updating isn't possible using the bindings.
Would it then maybe be possible to re-use (parts) of the connection that is set up by the bindings to reduce the plumbing needed in order to be able to use (for instance) the PowerShell module AzTable
to update a row?
So that we could do something like this:
$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
答案1
得分: 1
你很不幸,我恐怕不能帮你了。文档证实了你的猜测:
> 此输出绑定仅支持在表中创建新实体。如果你需要从函数代码中更新现有实体,而不是使用 Azure Tables SDK 直接操作。
英文:
You are out of luck I am afraid. The docs confirm your suspicion:
> This output binding only supports creating new entities in a table. If you need to update an existing entity from your function code, instead use an Azure Tables SDK directly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论