how to use nextmarker parameter for Azure list blobs rest api using PowerShell

1k views Asked by At

using the below script I am able to get the blobs but since I have more than 5000 items so need to use next marker to get all the result so needed a help forming $stringtosign for marker and $URL for next marker. Need help with code inside If ($nextMarker) . Reference : https://learn.microsoft.com/en-us/rest/api/storageservices/list-blobs

$StorageAccount = 'xxxxxxx' 
$ContainerName = 'xxxxxx' 
$accesskey = 'xxxxxxxxxxxxx'

$date = [System.DateTime]::UtcNow.ToString("R",[Globalization.CultureInfo]::InvariantCulture)

$version = '2019-12-12'
$stringToSign = "GET`n`n`n`n`n`n`n`n`n`n`n`n"+
           "x-ms-date:$date`nx-ms-version:$version`n"+
           "/$StorageAccount/$ContainerName`ncomp:list`nrestype:container" 
$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.blob.core.windows.net/" + $ContainerName + "?restype=container&comp=list"

$blobs = Invoke-RestMethod -method GET -Uri $URI -Headers $headers

$Result = [xml]($blobs -replace '',"")
#$Result.EnumerationResults.Blobs.Blob | % {$length = $length + $_.Properties.'Content-Length'}

$NextMarker = $Result.EnumerationResults.NextMarker

If ($NextMarker){

$stringToSign = "GET`n`n`n`n`n`n`n`n`n`n`n`n"+
           "x-ms-date:$date`nx-ms-version:$version`n"+
"/$StorageAccount/$ContainerName`ncomp:list`n`nrestype:container`nmarker:$NextMarker`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.blob.core.windows.net/" + $ContainerName + "?restype=container&comp=listmarker=$NextMarker&maxresults=5000"

$blobs = Invoke-RestMethod -method GET -Uri $URI -Headers $headers



}
1

There are 1 answers

0
Ivan Glasenberg On

In the If ($NextMarker){ } code block, please replace the $stringToSign filed with the following one:

$stringToSign = "GET`n`n`n`n`n`n`n`n`n`n`n`n"+
           "x-ms-date:$date`nx-ms-version:$version`n"+
           "/$StorageAccount/$ContainerName`ncomp:list`nmarker:$NextMarker`nmaxresults:5000`nrestype:container"

no other more changes. It works fine at my side.