如何为 Azure 存储构建授权 Header 获取容器 属性 REST API

How to contruct an Authorization Header for Azure Storage get container property REST API

我正在尝试使用 Azure 存储 Get Container Properties REST API. I follow the "Authentication for the Azure Storage Services" 为请求构建授权 Header。这是我使用的 PowerShell 脚本。

$StorageAccount = "<Storage Account Name>"
$Key = "<Storage Account Key>"
$resource = "<Container Name>"

$sharedKey = [System.Convert]::FromBase64String($Key)
$date = [System.DateTime]::UtcNow.ToString("R")

$stringToSign = "GET`n`n`n`n`n`n`n`n`n`n`n`nx-ms-date:$date`nx-ms-version:2009-09-19`n/$StorageAccount/$resource`nrestype:container"

$hasher = New-Object System.Security.Cryptography.HMACSHA256
$hasher.Key = $sharedKey

$signedSignature = [System.Convert]::ToBase64String($hasher.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($stringToSign)))

$authHeader = "SharedKey ${StorageAccount}:$signedSignature"

$headers = @{"x-ms-date"=$date
             "x-ms-version"="2009-09-19"
             "Authorization"=$authHeader}

$container = Invoke-RestMethod -method GET `
             -Uri "https://$StorageAccount.blob.core.windows.net/$resource?restype=container" `
             -Headers $headers

从上面的脚本中,我收到身份验证失败错误。授权 header 格式不正确。

知道如何解决这个问题吗?

好吧,我犯了一个非常愚蠢的错误。在我的 Invoke-RestMethod 的 URI 中,$resource?restype 被 PowerShell 识别为一个变量。由于未定义,URI 变为 https://$StorageAccount.blob.core.windows.net/=container。因此,认证总是失败。连接 URI 将解决问题。

$URI = "https://$StorageAccount.blob.core.windows.net/$resource"+"?restype=container"
$container = Invoke-RestMethod -method GET -Uri $URI -Headers $headers