2

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.

0

1 Answer 1

1

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  
} 
Sign up to request clarification or add additional context in comments.

7 Comments

Yeah really Thanks I just saw your code I am really happy Let me try fully and get back to you in case if I have doubts...
@ Gaurav Mantri - Really great thanks on that always 100% working code tested. Can just share the idea, on how the path can be parameterised.
Can you please explain what you mean by path can be parameterised?
Ok I can just brief out the scenario in short here so that you can share if any code in PS. The $path which is now looking for specific subfolder level and not azure blob content,great but the same till some extent $path = A/B/C till C it will be constant but after C subfolder it will look for dynamic download of contents/parameterization after C, it will have folder with 'daily dates' inside which it can have only 1 more subfolder. Now this - till C it should remain constant and then the PS code should be able to dwnload any date subfolder values, thereafter... Hope you got it ..
Old things are still got resolved by myself. Can anyone suggest, how to getdate and subtract it to previous date in PS code assign the current date to a variable.My current code is not allowing me to do that. $path = "A/B/C" (Get-Date).AddDays(-1).ToString("yyyy-mm-dd") Get-Date -Format "dddd MM/dd/yyyy HH:mm K" $blobs = Get-AzStorageBlob -Container $container_name -Prefix $path$date -Context $storage_account Quick help is great help for me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.