Read file data from Azure Storage FileShare

840 Views Asked by At

I am trying to read data from the files (json files) that exists in my Azure Storage's FileShare. I don't want to download the file and instead just read and get the data. The following command downloads the file for me instead of reading and storing the data in a variable. Please help.

$fileContents = Get-AzStorageFileContent -ShareName $fileShareName -Path $filePath -Context (Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name "name").Context -OutVariable fileContents

How can I read the data and potentially post files in the filshare as well? I have been following this documentation but could not find a solution.

1

There are 1 best solutions below

0
On

I tried in my environment and got below results:

Initially I tried with below commands to read the file from azure-file share with downloading it worked.

Commands:

$accountName = "venkat123"
$accountKey = "<storage account key >"
$fileshareName = "fileshare1"
$Name = "directory1/file.json"
$content="<downloaded path\file.json>"

$ctx = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $accountKey

Get-AzStorageFileContent -ShareName $fileShareName -Path $Name -Destination "file.json" -Context $ctx
Get-content -path $content

Console:

enter image description here

As workaround, if you need to read the files in azure file share without downloading you can use below python code.

Code:

from  azure.storage.file  import  FileService

storageAccount='venkat123'
accountKey='xxxxxxx'
file_service = FileService(account_name=storageAccount, account_key=accountKey)

share_name="fileshare1"

directory_name="directory1"

file_name="file.json"

file = file_service.get_file_to_text(share_name, directory_name, file_name)

print(file.content)

Console: enter image description here