anirudh bragadeesan
anirudh bragadeesan

Reputation: 35

PowerShell Code to Get the blob content to local

PowerShell Code to Get the blob content to local.

Explanation of scenario below-

I have code written me on how to download the blob folder level to the local & its working. But my main focus is as below suggest me how this can be achieved. storage account has container --folder1,2etc.. folder1 inside that will have dir1, 2 etc.., now i want to get the content of the particular dir1 from folder 1 using PS.

The already working code on folder level is below for reference - guide me on the next level.

Connect-AzAccount -Subscription "XXXX"

$container_name = 'A/B/C' 
$destination_path = "C:\Users\Anirudh\Documents\upload\"  
$storage_account = New-AzStorageContext -StorageAccountKey "XXXXX" -StorageAccountName "XXXXX"
$blobs = Get-AzStorageBlob -Container $container_name -Context $storage_account  
foreach($blob in $blobs) {  
    New-Item -ItemType Directory -Force -Path $destination_path  
    Get-AzStorageBlobContent -Container $container_name -Blob $blob.Name -Destination $destination_path -Context $storage_account  
} 

Thanks.

Upvotes: 2

Views: 995

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136336

If you want to download the blobs from a particular subfolder in a blob container, then you will need to specify the path to that subfolder in -Prefix parameter to Get-AzStorageBlob Cmdlet.

Here's the sample code I wrote which download blobs from A/B/C folder in test blob container in development storage account.

$container_name = "test" 
$destination_path = "D:\temp\"  
$storage_account = New-AzStorageContext -ConnectionString "UseDevelopmentStorage=true"
$path = "A/B/C" # This is the path of the sub folder inside the blob container.
$blobs = Get-AzStorageBlob -Container $container_name -Prefix $path -Context $storage_account  
foreach($blob in $blobs) {  
    New-Item -ItemType Directory -Force -Path $destination_path  
    Get-AzStorageBlobContent -Container $container_name -Blob $blob.Name -Destination $destination_path -Context $storage_account  
} 

Upvotes: 1

Related Questions