簡體   English   中英

在Powershell腳本中通過調用API在Azure Data Lake Gen2中創建文件系統

[英]Creating file system in azure data lake gen2 with calling API in powershell script

我正在嘗試在現有存儲帳戶(數據湖gen2)中創建文件系統。 我不知道我要執行的代碼是什么問題。 我一直在尋找解決方案,但找不到任何解決方案。

這是代碼:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

#name of created storage account
$accountName = 'multiprotocoladls2'
#name of file system that i want to create in existing storage account
$fsName= 'multiprotoadls2fs'

$token = 'here is my token'            
$header = @{
    "Content-Length"="0";
    "x-ms-version"="2018-11-09";
    "Authorization"="Bearer $token"
}

$uri = "https://$accountName.dfs.core.windows.net/" + $fsName + "?resource=filesystem" 

Write-Host $uri 

Invoke-RestMethod -Uri $uri -Method 'PUT' -Headers $header

我收到以下錯誤:

    Invoke-RestMethod : {"error":{"code":"AuthenticationFailed","message":"Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly includin
g the signature.\nRequestId:723b78a9-a01f-002e-6973-5801e3000000\nTime:2019-08-21T22:53:41.6191544Z"}}

我不知道為什么。 我檢查了標頭等中的參數,嘗試根據文檔進行所有操作。

https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/filesystem/create

誰能給我一個提示我如何克服這個問題?

這是身份驗證問題。

這是一篇有關如何在ADLS Gen2 api中使用ADLS Gen2 api的好文章 ,在我這方面效果很好。

我自己測試過的代碼:

$StorageAccountName="xxx"
$FilesystemName="xxx"
$AccessKey="xxx"

$date = [System.DateTime]::UtcNow.ToString("R") # ex: Sun, 10 Mar 2019 11:50:10 GMT

$n = "`n"
$method = "PUT"

$stringToSign = "$method$n" #VERB
$stringToSign += "$n" # Content-Encoding + "\n" +  
$stringToSign += "$n" # Content-Language + "\n" +  
$stringToSign += "$n" # Content-Length + "\n" +  
$stringToSign += "$n" # Content-MD5 + "\n" +  
$stringToSign += "$n" # Content-Type + "\n" +  
$stringToSign += "$n" # Date + "\n" +  
$stringToSign += "$n" # If-Modified-Since + "\n" +  
$stringToSign += "$n" # If-Match + "\n" +  
$stringToSign += "$n" # If-None-Match + "\n" +  
$stringToSign += "$n" # If-Unmodified-Since + "\n" +  
$stringToSign += "$n" # Range + "\n" + 
$stringToSign +=    
                    <# SECTION: CanonicalizedHeaders + "\n" #>
                    "x-ms-date:$date" + $n + 
                    "x-ms-version:2018-11-09" + $n # 
                    <# SECTION: CanonicalizedHeaders + "\n" #>

$stringToSign +=    
                    <# SECTION: CanonicalizedResource + "\n" #>
                    "/$StorageAccountName/$FilesystemName" + $n + 
                    "resource:filesystem"# 
                    <# SECTION: CanonicalizedResource + "\n" #>

$sharedKey = [System.Convert]::FromBase64String($AccessKey)
$hasher = New-Object System.Security.Cryptography.HMACSHA256
$hasher.Key = $sharedKey

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


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

$headers = @{"x-ms-date"=$date} 
$headers.Add("x-ms-version","2018-11-09")
$headers.Add("Authorization",$authHeader)

$URI = "https://$StorageAccountName.dfs.core.windows.net/" + $FilesystemName + "?resource=filesystem"

Try {
    Invoke-RestMethod -method $method -Uri $URI -Headers $headers # returns empty response
}
catch {
    $ErrorMessage = $_.Exception.Message
    $StatusDescription = $_.Exception.Response.StatusDescription
    $false

    Throw $ErrorMessage + " " + $StatusDescription
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM