我正在使用 Powershell 从 Azure 存储表读取数据。我想直接使用API而不需要任何模块。
我使用 SAS 令牌进行身份验证。
$SASToken = '?sv=2022-11-02&ss=bfqt&srt=s&sp=rwdlacupiytfx&se=2023-12-27T09:28:48Z&st=2023-12-27T01:28:48Z&spr=https&sig=2gqxxxxxx'
$GMTTime = (Get-Date).ToUniversalTime().toString('R')
$headers = @{
'x-ms-date' = $GMTTime;
Accept = 'application/json;odata=nometadata'
}
$url = "https://$StorageAccountName.table.core.windows.net/${TableName}${SASToken}"
$response = Invoke-WebRequest -Uri $url -Headers $headers -Method Get -ErrorAction Stop
$NextRowKey = $response.Headers.'x-ms-continuation-NextRowKey'
$NextPartitionKey = $response.Headers.'x-ms-continuation-NextPartitionKey'
第一个调用按预期工作。
我正在阅读 文档,但我仍然不知道如何构建下一页(1000 个条目)的 URL。
我试过了
$url = "http://$StorageAccountName.table.core.windows.net/${TableName}${SASToken}?NextPartitionKey=$NextPartitionKey&NextRowKey=$NextRowKey"
Invoke-WebRequest -Uri $url -Headers $headers -Method Get -ErrorAction Stop
Invoke-WebRequest:
AuthenticationFailed
Server failed to authenticate the request. Make sure the value of the Authorization header is formed correctly including the signature.
没有一个变量为空,并且所有变量都有正确的值。
Invoke-WebRequest:AuthenticationFailed 服务器无法验证请求。确保授权标头的值格式正确,包括签名。
当您传递了错误的参数或无效的 sas 令牌时,就会发生上述错误,我也在您的
url
中看到您在脚本中传递了两次?
,这使您收到身份验证错误。
这是完整的脚本,它在我的环境中有效。
脚本:
$StorageAccountName="xxxx"
$TableName="xxx"
$SASToken = '?sv=2022-11-02&ss=bfqt&srt=co&sp=rwdlacupiytfx&se=2023-12-27T13:35:21Z&st=2023-12-27T05:35:21Z&spr=https&sig=xxxxx'
$GMTTime = (Get-Date).ToUniversalTime().toString('R')
$headers = @{
'x-ms-date' = $GMTTime;
Accept = 'application/json;odata=nometadata'
}
$url = "https://$StorageAccountName.table.core.windows.net/${TableName}${SASToken}"
$response = Invoke-WebRequest -Uri $url -Headers $headers -Method Get -ErrorAction Stop
$NextRowKey = $response.Headers.'x-ms-continuation-NextRowKey'
$NextPartitionKey = $response.Headers.'x-ms-continuation-NextPartitionKey'
while ($NextRowKey -ne $null -and $NextPartitionKey -ne $null) {
$url = "https://$StorageAccountName.table.core.windows.net/${TableName}${SASToken}&NextPartitionKey=$NextPartitionKey&NextRowKey=$NextRowKey"
$response = Invoke-WebRequest -Uri $url -Headers $headers -Method Get -ErrorAction Stop
$NextRowKey = $response.Headers.'x-ms-continuation-NextRowKey'
$NextPartitionKey = $response.Headers.'x-ms-continuation-NextPartitionKey'
$content = $response.Content | ConvertFrom-Json
foreach ($entity in $content.value) {
Write-Output "PartitionKey: $($entity.PartitionKey), RowKey: $($entity.RowKey)"
}
}
输出:
PartitionKey: Partition1, RowKey: Row1899
PartitionKey: Partition1, RowKey: Row19
PartitionKey: Partition1, RowKey: Row190
PartitionKey: Partition1, RowKey: Row1900
PartitionKey: Partition1, RowKey: Row1901
PartitionKey: Partition1, RowKey: Row1902
PartitionKey: Partition1, RowKey: Row1903
PartitionKey: Partition1, RowKey: Row1904
PartitionKey: Partition1, RowKey: Row1905
PartitionKey: Partition1, RowKey: Row1906
PartitionKey: Partition1, RowKey: Row1907
PartitionKey: Partition1, RowKey: Row1908
参考: