Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kevinhillinger/72ff39c1ed57694d05cbbccb8b7852d2 to your computer and use it in GitHub Desktop.
Save kevinhillinger/72ff39c1ed57694d05cbbccb8b7852d2 to your computer and use it in GitHub Desktop.
Using ListBlobsSegmented to page through Azure Blob Storage results
# get the container
$account = Get-AzureRmStorageAccount -Name blobiterationtest0 -ResourceGroupName blob-iteration-test
$context = $account.Context
$container = Get-AzureStorageContainer -Context $context -Name test
$maxResults = 5
$segment = 0 #which segment / result set are we getting? (zero index)
function getBlobs($segment) {
$blobPrefix = $null
$useFlatBlobListing = $true
$blobListingDetails = [Microsoft.WindowsAzure.Storage.Blob.BlobListingDetails]::Metadata
$continuationToken = $null
$operationContext = New-Object -TypeName Microsoft.WindowsAzure.Storage.OperationContext
$blobRequestOptions = New-Object -TypeName Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions
$blobResultSegment = $null
$isLasResultSet = $false
for ($i = 0; $i -le $segment; $i++) {
if ($isLasResultSet) {
$blobResultSegment = $null
break;
}
$blobResultSegment = $container.CloudBlobContainer.ListBlobsSegmented(
$blobPrefix,
$useFlatBlobListing,
$blobListingDetails,
$maxResults,
$continuationToken,
$blobRequestOptions,
$operationContext
)
$continuationToken = $blobResultSegment.ContinuationToken
if ($continuationToken -eq $null) { # no results remaining
$isLasResultSet = $true;
}
}
return $blobResultSegment.Results
}
# now operate on the blobs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment