Norrin Rad
Norrin Rad

Reputation: 991

Azure PowerShell Download blob contents from container

I'm trying to download sme blob files from an azure storage account.

There are a mix of other containers and blockblobs in the parent container, I need to download just the blockblobs and not the other containers, I can't find a way of seperating them out, also I need to download some blobs from a conatiner within a container.

My code will download all the contents in the parent blob, including all sub containers.

  $sub = "MySub"
$staccname = "straccname1234"
$key = "sdcsecurekeythinghere"
$ctx = New-AzureStorageContext -StorageAccountName $staccname ` 
         -StorageAccountKey $key
$cont = "data\download\files.001" ##the container includes other cntainers and subcontainers
$dest = "C:\blb-Downloads"
Select-AzureSubscription -SubscriptionName $sub –Default
Set-AzureSubscription -Currentstaccname $staccname -SubscriptionName $sub
Get-AzureStorageBlob -Container $cont -Context $ctx
$blobs = Get-AzureStorageBlob -Container $cont  -Context $ctx
$blobs | Get-AzureStorageBlobContent –Destination $dest  -Context $ctx

There are approx 75 files in the parent blob and 123 files in data\downloads.

Upvotes: 2

Views: 6075

Answers (3)

jawad846
jawad846

Reputation: 772

This works for me

$storageaccountname = "jawadtestsaacc"
$sastoken = ""
$ContainerName = "access"

$Ctx = New-AzStorageContext -StorageAccountName $storageaccountname -SasToken $sastoken

Get-AzStorageBlob -Container "$ContainerName" -Blob "Az.json" -Context $Ctx | Get-AzStorageBlobContent
or
Get-AzStorageBlobContent -Container "$ContainerName" -Blob "Az.json" -Context $Ctx

Upvotes: 0

RoadRunner
RoadRunner

Reputation: 26315

Using the newer Azure PowerShell Az module, you can use Get-AzStorageBlob to list all the block blobs from the container, then use Get-AzStorageBlobContent to download the blobs.

As already shown by @George Wallace, we can use Where-Object or its alias ? to filter block blob types.

Demo:

$resourceGroup = "myResourceGroup"
$storageAccount = "myStorageAccount"
$container = "myContainerName"
$destination = "./blobs"

# Create destination directory if it doesn't exist
if (-not (Test-Path -Path $destination -PathType Container)) {
    New-Item -Path $destination -ItemType Directory
}

# Get storage account with container we want to download blobs from
$storageAccount = Get-AzStorageAccount -Name $storageAccount -ResourceGroupName $resourceGroup

# Get all BlockBlobs from container
$blockBlobs = Get-AzStorageBlob -Container $container -Context $storageAccount.Context 
    | Where-Object {$_.BlobType -eq "BlockBlob"}

# Download each blob from container into destination directory
$blockBlobs | Get-AzStorageBlobContent -Destination $destination -Force

Upvotes: 4

George Wallace
George Wallace

Reputation: 146

Can you not just run the following and limit it to BlockBlobs only?

Get-AzureStorageBlob -Container $cont  -Context $ctx | ? {$_.BlobType -eq "BlockBlob"}

Upvotes: 2

Related Questions