如何使用Azure函数的输出绑定更新Azure存储表中的行

huangapple go评论66阅读模式
英文:

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:

    {
      &quot;tableName&quot;: &quot;tobeprocessed&quot;,
      &quot;connection&quot;: &quot;ProcessStorage&quot;,
      &quot;name&quot;: &quot;InProcessStorageTableBinding&quot;,
      &quot;type&quot;: &quot;table&quot;,
      &quot;direction&quot;: &quot;in&quot;
    },
    {
      &quot;tableName&quot;: &quot;tobeprocessed&quot;,
      &quot;connection&quot;: &quot;ProcessStorage&quot;,
      &quot;name&quot;: &quot;OutProcessStorageTableBinding&quot;,
      &quot;type&quot;: &quot;table&quot;,
      &quot;direction&quot;: &quot;out&quot;
    }

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 &lt;filter to retrieve row&gt;

$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.

huangapple
  • 本文由 发表于 2023年4月16日 23:12:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定