1
votes

Can a previous version of an Azure Storage Blob, with blob versioning enabled, be downloaded using Powershell or Azure CLI (or other) ?

In the Azure Portal, with Blob versioning enabled, you can download the previous versions, as in the image below:

Downloading previous version from Azure portal

Using Powershell, I can retrieve/download a list of previous versions with the Get-AZStorageBlob -IncludeVersion paramater

PS W:\SRE\KeyVaultBackupPOC> $blob = Get-AzStorageBlob -Container blobversiontest -Context $StorageContext -IncludeVersion   

PS W:\SRE\KeyVaultBackupPOC> $blob


   AccountName: blobstorage, ContainerName: blobversiontest

Name                 BlobType  Length          ContentType                    LastModified         AccessTier SnapshotTime                 IsDeleted  VersionId
----                 --------  ------          -----------                    ------------         ---------- ------------                 ---------  ---------
BlobVersionTestFile… BlockBlob 26              text/plain                     2021-03-12 03:44:16Z Hot                                     False      2021-03-12T03:44:16.4786504Z
BlobVersionTestFile… BlockBlob 25              text/plain                     2021-03-12 03:44:36Z Hot                                     False      2021-03-12T03:44:36.8181879Z
BlobVersionTestFile… BlockBlob 24              text/plain                     2021-03-12 03:44:57Z Hot                                     False      2021-03-12T03:44:57.4459306Z *

However, I cannot find a way to retrieve the previous version, via script.

I can retrieve an ICloud blob object for the current version of the blob using Get-AzStorageBlob and the ICloudBlob property of the result, but if I try a similar method for a previous version of the blob, I get an error:

Get-AzStorageBlobContent: Object 'CloudBlob' cannot be null. (Parameter 'CloudBlob')

Hoping for some new insights - thanks in advance.

2

2 Answers

2
votes

If you want to download blob with one version, please refer to the following script

$ctx=New-AzStorageContext -StorageAccountName ""   -StorageAccountKey ""

Get-AzStorageBlob -Blob $blobName -VersionId "<the version id you need>" -Container "test" -Context $ctx


$blob | Get-AzStorageBlobContent -Destination e:\
0
votes

You can filter out the previous blobs by checking against IsLatestVersion, then selecting the last blob as the previous blob. Then you can download the blob with Get-AzStorageBlobContent using the pipeline.

$storageAccountName = "myStorageAccount"
$resourceGroupName = "myStorageAccountRg"
$containerName = "myContainer"
$blobName = "myblob.txt"

$storageAccount = Get-AzStorageAccount `
    -Name $storageAccountName `
    -ResourceGroupName $resourceGroupName

$previousBlob = Get-AzStorageBlob `
    -Container $containerName `
    -Context $storageAccount.Context `
    -IncludeVersion | 
        Where-Object {
            -not $_.IsLatestVersion -and 
            $_.Name -eq $blobName
        } | Select-Object -Last 1

$previousBlob | Get-AzStorageBlobContent -Destination $PSScriptRoot -Force