Eddie Kumar
Eddie Kumar

Reputation: 1490

Azure File Storage - List directory contents - read filenames

This sounds like an easiest task, all I'm trying to do is to list the contents of a sub-directory under a Share on Azure file storage, I managed to get the "CloudFileDirectory" object, however, struggling to read its contents.

I am limited to write this solution in PowerShell, hence using a PS module called "AzureRM" as below:

# Install AzureRM module & check if installed:
Install-Module -Name "AzureRM"
Get-Module -Name AzureRM -List | select Name,Version

# Import module in session:
Import-Module AzureRM

# Connect/sign-on to Azure account (interactive login):
Connect-AzureRmAccount

# Get reference to Storage account:
$storageAcct = Get-AzureRmStorageAccount -ResourceGroupName "myRSG" -AccountName "myCloudStorage"

#Get directory object:
$objDir = Get-AzureStorageFile -Context $storageAcct.Context -ShareName "myShare" -Path "myFolder1"

Here I'm stuck with this ($objDir) object which is basically a WindowsAzure.Storage.File.CloudFileDirectory object, but doesn't act like a usual directory object, as it doesn't appear to contain a Collection (see output below):

$objDir.GetType().FullName           #-> "WindowsAzure.Storage.File.CloudFileDirectory" object
$objDir | % {$_.GetType().FullName}  #-> "WindowsAzure.Storage.File.CloudFileDirectory" object

Basically, I need to read the file-names in this sub-folder. I will appreciate any help. TIA.

Upvotes: 0

Views: 2012

Answers (1)

Eddie Kumar
Eddie Kumar

Reputation: 1490

(As per Gaurav's comment) piping an additional | Get-AzureStorageFile resolved the issue and returned expected output, i.e. I'm now getting a collection/array (System.Object[]) which I iterated through to get individual file-names and their respective sizes, here is the code+output and the object types I'm now getting:

#Get directory object:
$objDir = Get-AzureStorageFile -Context $storageAcct.Context -ShareName "myShare" -Path "myFolder1" | Get-AzureStorageFile
$objDir.GetType().FullName            #-> "System.Object[]" object
$objDir  | % {$_.GetType().FullName}  #-> "WindowsAzure.Storage.File.CloudFile / CloudDirectory" objects
$objDir  | % {$_} | select Name,StreamMinimumReadSizeInBytes

Output

Name      StreamMinimumReadSizeInBytes
----      ----------------------------
file.txt                       4194304
file1.txt                      4194304
foo.txt                        4194304

Upvotes: 1

Related Questions