如何在 Azure 文件共享列表 REST 中使用 maxresult 和 nextmarker 参数 API

How to use maxresult and nextmarker parameter in Azure File Share List REST API

我正在尝试使用 List Share REST API (https://docs.microsoft.com/en-us/rest/api/storageservices/list-shares#Authorization) 列出存储帐户中存在的所有共享。但我在使用标记参数的授权 header 时遇到问题。我猜问题出在 $nextmarker If 语句下的 $stringtosign 变量。

$StorageAccount = "XXXXX"
$Accesskey = "XXXXXXX==";
$Version="2019-12-12"
$SharePropObj = @()
$date = [System.DateTime]::UtcNow.ToString("R",[Globalization.CultureInfo]::InvariantCulture)

$stringToSign = "GET`n`n`n`n`n`n`n`n`n`n`n`n"+
           "x-ms-date:$date`nx-ms-version:$version`n" +
           "/$storageAccount/`ncomp:list`nmaxresults:5000" 
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Convert]::FromBase64String($accesskey)
$signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($stringToSign))
$signature = [Convert]::ToBase64String($signature)

$headers=@{"x-ms-date"=$date;
           "x-ms-version"= $version;
           "Authorization"= "SharedKey $($storageAccount):$signature";
           "ContentType"="application/xml"
}
$URI = "https://$storageAccount.file.core.windows.net/?comp=list&maxresults=5000"

Try {
    $response = Invoke-RestMethod $URI -Method 'GET' -Headers $headers
    
}Catch {
    "Error Occurred. $_.exception.message"
    }
If ($response){

[xml]$Result = [xml]($response -replace '',"")
    
    $Shareprops = $Result.EnumerationResults.ChildNodes.share | %{

        $share = $_.Name
        $Quota = $_.properties.Quota
        "$share,$Quota"
        }

        $SharePropCol = "ShareName,Quota"
        $SharePropObj += @($SharePropCol,($Shareprops | where {$_.length -gt 1})) | ConvertFrom-Csv -Delimiter ","

        $SharePropObj
        

   $NextMarker = $Result.EnumerationResults.NextMarker.Split("/")[-1]

   if ($NextMarker){

          #Write-Error "Data for some shares are missed"
          #$SharePropObj = @()
          $date = [System.DateTime]::UtcNow.ToString("R",[Globalization.CultureInfo]::InvariantCulture)

        $stringToSign = "GET`n`n`n`n`n`n`n`n`n`n`n`n"+
                   "x-ms-date:$date`nx-ms-version:$version`n" +
                   "/$storageAccount/`ncomp:list`nmaxresults:5000`nmarker:$NextMarker" 
        $hmacsha = New-Object System.Security.Cryptography.HMACSHA256
        $hmacsha.key = [Convert]::FromBase64String($accesskey)
        $signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($stringToSign))
        $signature = [Convert]::ToBase64String($signature)

        $headers=@{"x-ms-date"=$date;
                   "x-ms-version"= $version;
                   "Authorization"= "SharedKey $($storageAccount):$signature";
                   "ContentType"="application/xml"
        }
        $URI = "https://$storageAccount.file.core.windows.net/?comp=list&maxresults=5000&marker=$NextMarker"

            $response1 = Invoke-RestMethod $URI -Method 'GET' -Headers $headers
            [xml]$Result = [xml]($response1 -replace '',"")
    
    $Shareprops = $Result.EnumerationResults.ChildNodes.share | %{

        $share = $_.Name
        $Quota = $_.properties.Quota
        "$share,$Quota"
        }


        $SharePropCol = "ShareName,Quota"
        $SharePropObj += @($SharePropCol,($Shareprops | where {$_.length -gt 1})) | ConvertFrom-Csv -Delimiter ","

        $SharePropObj
    
       
   
   
   }

    }

请更改您的以下代码:

1.for$NextMarker,请修改这行代码

$NextMarker = $Result.EnumerationResults.NextMarker.Split("/")[-1]

$NextMarker = "/$storageAccount/" + $Result.EnumerationResults.NextMarker.Split("/")[-1]
  1. if ($NextMarker){} 代码块中 -> 对于 $stringToSign,请将 marker:$NextMarker 放在 maxresults:5000。像下面这样更改它:

    $stringToSign = "GET`n`n`n`n`n`n`n`n`n`n`n`n"+
                "x-ms-date:$date`nx-ms-version:$version`n" +
                "/$storageAccount/`ncomp:list`nmarker:$NextMarker`nmaxresults:5000".
    

3.in if ($NextMarker){} 代码块 -> 对于 $URI,请同时将 marker=$NextMarker 放在 maxresults=5000。像下面这样更改它:

$URI = "https://$storageAccount.file.core.windows.net/?comp=list&marker=$NextMarker&maxresults=5000"

我已经测试过了,效果很好。如果您仍有问题,请告诉我。