PowerShell script to download files from a particular folder in blob storage container

Viewed 889

I am writing a power shell script to download files from a azure blob storage container.The files are inside a folder in the container. The blob container structure looks like A>B>C.csv(Where A is my container). I want to download only C.csv to my local folder. I wrote script as below.

    $AccountName = "X"
    $ContainerName = "A"
    $Key="dcg"
    $Context= New-AzureStorageContext -StorageAccountName $AccountName -StorageAccountKey $Key
    $Files=Get-AzureStorageBlob -Container $ContainerName  -Context $Context -prefix "A/B"| 
     Where-Object {($_.ContentType -eq "application/octet-stream")}
    $DestinationPath = "D:\localfolder"
    $files |Get-AzureStorageBlobContent -Destination $DestinationPath -Context $Context

But this script copy entire folder structure to my local folder. So my local folder contains LocalFolder>A>B>C.csv, but I need only C.csv to be downloaded(LocalFolder>C.csv). Can anyone please help me to resolve this issue?

1 Answers

I don't see a way to download the blob with out a folder structure using power shell script. I tried the below script and getting the same output

$AccountName = "*******"
$ContainerName = "*******"
$BlobName = "*******" 
$localTargetDirectory ="*******"
$Key="*******"
$ctx= New-AzureStorageContext -StorageAccountName $AccountName -StorageAccountKey $Key
Get-AzureStorageBlobContent -Blob $BlobName -Container $ContainerName `
    -Destination $localTargetDirectory -Context $ctx

Other options we have is like azure storage explore and AZ copy to download the file directly with out folder structure. here are the reference doc's AZ Copy Storage Explorer

Related