2016-04-07 52 views
3

我正在嘗試使用Azure存儲Get Container Properties REST API。我按照「Authentication for the Azure Storage Services」爲請求構建授權標頭。這是我使用的PowerShell腳本。如何構建Azure存儲的授權標頭獲取容器屬性REST API

$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 

從上面的腳本中,我得到身份驗證失敗的錯誤。授權標頭未正確形成。

關於如何解決這個問題的任何想法?

回答

3

嗯,我犯了一個非常愚蠢的錯誤。在我的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