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

问题描述

我正在尝试使用列表共享REST API(https://docs.microsoft.com/en-us/rest/api/storageservices/list-shares#Authorization)列出存储帐户中存在的所有共享。但我在带有标记参数的Authorization标头中遇到问题。我猜问题出在$ 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 '',$Quota"
        }


        $SharePropCol = "ShareName,"

        $SharePropObj
    
       
   
   
   }

    }

解决方法

请更改您的以下代码:

1。对于$NextMarker,请更改此代码行

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

$NextMarker = "/$storageAccount/" + $Result.EnumerationResults.NextMarker.Split("/")[-1]
    if ($NextMarker){}代码块中的
  1. -> $stringToSign中,请在{strong> 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。在if ($NextMarker){}代码块->中,将$URI放置在{strong> marker=$NextMarker之前的maxresults=5000之前。如下更改:

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

我已经对其进行了测试,并且效果很好。如果您仍有问题,请告诉我。