gpt4 book ai didi

azure - 通过 PowerShell 在 ADLS Gen2 中重命名文件名时出现异常

转载 作者:行者123 更新时间:2023-12-03 05:41:32 30 4
gpt4 key购买 nike

我可以使用 PowerShell 和 ADLS Gen 2 REST API 在 ADLS 中创建文件和文件夹。但是我在重命名该文件时遇到问题。我在 header 中使用“x-ms-rename-source”,但它抛出异常。

代码:

$n = '`n'
$stringToSign +=
#SECTION: CanonicalizedHeaders + “\n” #
“x-ms-date:$date” + $n +
“x-ms-version:2018-11-09” + $n +
“x-ms-rename-source:/adlsg2filesystemname/folderpath/filename” + $n
$stringToSign +=
# SECTION: CanonicalizedResource + “\n” #
“/$StorageAccountName/$FilesystemName” + $PathToCreate + $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(“x-ms-rename-source”,”/adlsg2filesystemname/folderpath/filename”)
$headers.Add(“Authorization”,$authHeader)
$headers.Add(“If-None-Match”,”*”) # To fail if the destination already exists, use a conditional request with If-None-Match: “*”

$URI = “https://$StorageAccountName.dfs.core.windows.net/” + $FilesystemName + $PathToCreate

我遇到以下异常:

Invoke-RestMethod : {"error":{"code":"AuthenticationFailed","message":"Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly 
including the signature.\nRequestId:ddfd851b-501f-0057-3f88-7e0a7d000000\nTime:2019-10-09T09:59:53.7708781Z"}}

任何帮助将不胜感激。谢谢。

最佳答案

根据我的测试,我们可以使用Azure AD身份验证来调用Azure数据湖存储Gen2 REST API。更多详情请引用https://social.msdn.microsoft.com/Forums/en-US/45be0931-379d-4252-9d20-164261cc64c5/error-while-calling-adls-gen-2-rest-api-to-create-file?forum=AzureDataLake .

  1. 创建 Azure AD 服务主体并为其分配 RABC 角色。更多信息请引用https://learn.microsoft.com/en-us/azure/storage/common/storage-auth-aad .
Connect-AzAccount
$password=''
$credentials = New-Object Microsoft.Azure.Commands.ActiveDirectory.PSADPasswordCredential -Property @{ StartDate=Get-Date; EndDate=Get-Date -Year 2024; Password=$password}
$sp = New-AzAdServicePrincipal -DisplayName jimtest1 -PasswordCredential $credentials

New-AzRoleAssignment -ApplicationId $sp.ApplicationId -RoleDefinitionName "Storage Blob Data Owner" -Scope "your scope such as your storage account scope"
  • 获取访问 token
  • $TeantID='hanxia.onmicrosoft.com'
    $TokenResult = Invoke-RestMethod -Method Post -ContentType 'application/x-www-form-urlencoded' -Uri "https://login.microsoftonline.com/$($TeantID)/oauth2/token" -Body @{
    client_id = $sp.ApplicationId # the application id of service principal
    resource = 'https://storage.azure.com'
    grant_type = 'client_credentials'
    client_secret = $password # you use it in step 1

    }
  • 调用其余API
  • $StorageAccountName =''
    $FilesystemName =''
    $PathToCreate=''
    $URI = “https://$StorageAccountName.dfs.core.windows.net/” + $FilesystemName +"/"+$PathToCreate
    Invoke-RestMethod -Method Put -Uri $URI -Headers @{
    'Authorization' = "Bearer "+ $TokenResult.access_token
    'x-ms-rename-source' = ' '
    }

    enter image description here enter image description here

    关于azure - 通过 PowerShell 在 ADLS Gen2 中重命名文件名时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58301873/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com