Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Standard disclaimer : This code is provided as a sample and is in no way warranted for production use.
- ## Set the storage context.
- $AccountName = ""
- $AccountKey = ""
- $ctx = New-AzureStorageContext -StorageAccountName $AccountName -StorageAccountKey $AccountKey
- ## Fetch a container to list. Note that I use the analytical logs folder. You are free to choose your own.
- $container = Get-AzureStorageContainer -Context $ctx -Name "`$logs"
- ## To fetch blob list as a flat structure (equivalent to dir /s /b)
- # 1. Listing the blobs with use flat listing set to true
- $container.CloudBlobContainer.ListBlobs($null,$true) | Foreach-Object {Write-Host $_.Uri.ToString()}
- ## To fetch blob list as folder structures
- # 1. Create a stack to walk the folder tree
- $folderstack = New-Object "System.Collections.Stack"
- $folderstack.Clear();
- # 2. Push in the top container
- $folderstack.Push($container)
- # 3. Start loop until stack is empty
- do{
- # 3.1. Pop entries from the stack to enumerate
- $c = $folderstack.Pop()
- # 3.2. The list method to call is different for an AzureStorageContainer object
- if ($c.GetType().Name -eq "AzureStorageContainer"){
- # 3.2.1. If the entries are directory objects, push them into the stack to enumerate again
- $c.CloudBlobContainer.ListBlobs($null,$false)| ForEach-Object {if ($_.GetType().Name -eq "CloudBlobDirectory") { $folderstack.Push($_); Write-Host $_.Uri.ToString()} else {Write-Host $_.Uri.ToString()}}
- }
- # 3.3. And different again for a CloudBlobDirectory object
- if ($c.GetType().Name -eq "CloudBlobDirectory")
- {
- # 3.3.1. If the entries are directory objects, push them into the stack to enumerate again
- $c.ListBlobs($false)| ForEach-Object {if ($_.GetType().Name -eq "CloudBlobDirectory") { $folderstack.Push($_); Write-Host $_.Uri.ToString()} else {Write-Host $_.Uri.ToString()}}
- }
- }while ($folderstack.Count -gt 1)
- # 4. Stack is empty, all entries have been enumerated!
- # All Done!
Add Comment
Please, Sign In to add comment