如何使用HTTP触发器PowerShell Azure功能从Azure表存储中读取?

时间:2017-07-04 16:11:37

标签: powershell azure azure-functions

Row Key将在查询字符串中传递。创建"连接字符串"的功能需要什么?到表存储?

1 个答案:

答案 0 :(得分:4)

假设您的功能应用程序中已有一个名为AzureWebJobsStorage的应用程序设置,其中包含连接字符串到表存储,然后要在PowerShell脚本中检索该值,您将添加以下内容,

$connectionString = $env:AzureWebJobsStorage;

但是,如果您只需要根据行键写入表存储,则可以利用Azure功能中已支持的表存储绑定。

假设您的表存储中已经创建了一个名为testtable的表,这是我们需要写入的表。然后,这是一个示例设置,它从HTTP触发器的查询字符串中读取行键并将条目写入表存储。

<强> function.json:

{
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "authLevel": "anonymous"
    },
    {
      "type": "table",
      "name": "outputTable",
      "tableName": "testtable",
      "connection": "AzureWebJobsStorage",
      "direction": "out"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}

<强> run.ps1:

# POST method: $req
$requestBody = Get-Content $req -Raw | ConvertFrom-Json
$name = $requestBody.name

# GET method: each querystring parameter is its own variable
if ($req_query_name) 
{
    $name = $req_query_name 
}

Out-File -Encoding Ascii -FilePath $res -inputObject "Hello $name"

Write-Output "Message entity: '$requestBody'"
$entity = [PSObject]@{
  PartitionKey = $requestBody.role  
  RowKey = $req_query_rowkey
  AccountId = $requestBody.id
}

$entity | ConvertTo-Json | Out-File -Encoding UTF8 $outputTable

邮递员测试:

enter image description here

日志视图:

2017-07-04T17:21:17.095 Function started (Id=775a36ce-9d71-454c-887c-05f08cfdb877)
2017-07-04T17:21:17.314 Message entity: '@{name=Azure; role=admin; id=78910}'
2017-07-04T17:21:17.314 Function completed (Success, Id=775a36ce-9d71-454c-887c-05f08cfdb877, Duration=222ms)

Azure存储资源管理器中的表条目视图:

enter image description here