0

I am using Powershell to read data from an Azure Storage Table. I want to use the API directly without any modules.
I use a SAS token for authentication.

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

This first call works as expected.
I was reading the documentation and I still cannot figure out how to construct the URL for the next page (1000 entries).
I tried

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

None of the variables are empty and all of them have the correct value.

1 Answer 1

0

Invoke-WebRequest: AuthenticationFailed Server failed to authenticate the request. Make sure the value of the Authorization header is formed correctly including the signature.

The above error occurred when you passed the wrong parameter or invalid sas token, also I see in your url you have passed two times ? in the script which makes you get an authentication error.

Here is the full script, Which worked in my environment.

Script:

$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)"
    }
}

Output:

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

enter image description here

Reference:

Query timeout and pagination (REST API) - Azure Storage | Microsoft Learn

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.