行键
将在查询字符串中传递。创建表存储的“连接字符串”的函数需要什么?
最佳答案
假设您的函数应用程序中已有一个名为 AzureWebJobsStorage
的应用程序设置,该设置具有到表存储的连接字符串,然后要在 PowerShell 脚本中检索该值,您将添加以下内容:
$connectionString = $env:AzureWebJobsStorage;
但是,如果您只需根据行键写入表存储,则可以利用 Azure Functions 中已支持的表存储绑定(bind)。
假设您的表存储中已创建一个名为 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
在 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)
Azure 存储资源管理器中的表条目 View :
关于powershell - 如何使用 HTTP 触发器 PowerShell Azure Function 读取 Azure 表存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44910406/