如何使用 HTTP 触发器 PowerShell Azure 函数从 Azure Table 存储读取数据?
How to read from Azure Table Storage with a HTTP Trigger PowerShell Azure Function?
Row Key
将在查询字符串中传递。创建 "connection string" 到 Table 存储的函数需要什么?
假设您的函数应用程序中已经有一个名为 AzureWebJobsStorage
的应用程序设置,它具有到您的 Table 存储的连接字符串,然后要在您的 PowerShell 脚本中检索该值,您将添加以下,
$connectionString = $env:AzureWebJobsStorage;
但是,如果您只需要根据行键写入 Table 存储,您可以利用 Azure Functions 中已经支持的 Table 存储绑定。
假设在您的 Table 存储中已经创建了一个名为 testtable
的 table,这就是我们需要写入的 table。然后,这是一个示例设置,它从 HTTP 触发器的查询字符串中读取行键并将条目写入 Table 存储。
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
}
运行.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
在 Postman 中测试:
日志查看:
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)
Table Azure 存储资源管理器中的条目视图:
Row Key
将在查询字符串中传递。创建 "connection string" 到 Table 存储的函数需要什么?
假设您的函数应用程序中已经有一个名为 AzureWebJobsStorage
的应用程序设置,它具有到您的 Table 存储的连接字符串,然后要在您的 PowerShell 脚本中检索该值,您将添加以下,
$connectionString = $env:AzureWebJobsStorage;
但是,如果您只需要根据行键写入 Table 存储,您可以利用 Azure Functions 中已经支持的 Table 存储绑定。
假设在您的 Table 存储中已经创建了一个名为 testtable
的 table,这就是我们需要写入的 table。然后,这是一个示例设置,它从 HTTP 触发器的查询字符串中读取行键并将条目写入 Table 存储。
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
}
运行.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
在 Postman 中测试:
日志查看:
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)
Table Azure 存储资源管理器中的条目视图: