如何构建Azure存储的授权标头获取容器属性REST API

时间:2016-04-07 01:07:41

标签: rest azure azure-storage-blobs azure-powershell

我正在尝试使用Azure存储https://github.com/apache/hadoop。我按照“Get Container Properties REST API”构建请求的授权标头。这是我使用的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

从上面的脚本中,我得到身份验证失败错误。未正确形成Authorization标头。

有关如何解决这个问题的想法吗?

1 个答案:

答案 0 :(得分:4)

好吧,我犯了一个非常愚蠢的错误。在我的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