2016-04-07 7 views
3

AzureストレージGet Container Properties REST APIを使用しようとしています。私は、 "Authentication for the Azure Storage Services"に従って、リクエスト用の認証ヘッダを作成します。ここで私が使用するPowerShellスクリプトです。Azure Storageの承認ヘッダを取得する方法コンテナプロパティ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 

上記のスクリプトから、認証に失敗しました。エラーです。 Authorizationヘッダーが正しく形成されていません。

これを修正する方法はありますか?

答えて

3

まあ、私は非常にばかげたエラーを作りました。 Invoke-RestMethodというURIでは、$resource?restypeはPowerShellによって1つの変数として認識されます。定義されていないため、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