【问题标题】:Download Previous Version of Azure Storage Blob Content Programmatically以编程方式下载 Azure 存储 Blob 内容的早期版本
【发布时间】:2021-03-15 15:33:59
【问题描述】:

可以使用 Powershell 或 Azure CLI(或其他)下载启用 Blob 版本控制的 Azure 存储 Blob 的早期版本吗?

在 Azure 门户中,启用 Blob 版本控制后,您可以下载以前的版本,如下图所示:

Downloading previous version from Azure portal

使用 Powershell,我可以使用 Get-AZStorageBlob -IncludeVersion 参数检索/下载以前版本的列表

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 *

但是,我找不到通过脚本检索以前版本的方法。

我可以使用Get-AzStorageBlob 和结果的 ICloudBlob 属性为当前版本的 blob 检索 ICloud blob 对象,但如果我尝试对以前版本的 blob 使用类似的方法,我会收到错误:

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

希望获得一些新见解 - 提前致谢。

【问题讨论】:

    标签: azure powershell azure-cli


    【解决方案1】:

    如果你想下载一个版本的blob,请参考以下脚本

    $ctx=New-AzStorageContext -StorageAccountName ""   -StorageAccountKey ""
    
    Get-AzStorageBlob -Blob $blobName -VersionId "<the version id you need>" -Container "test" -Context $ctx
    
    
    $blob | Get-AzStorageBlobContent -Destination e:\
    

    【讨论】:

    • 谢谢@Jim Xu。这适用于所有版本。我的方法是缺少管道内容的 blob。
    【解决方案2】:

    您可以通过检查IsLatestVersion 来过滤掉以前的 blob,然后选择最后一个 blob 作为前一个 blob。然后您可以使用管道下载带有Get-AzStorageBlobContent 的blob。

    $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
    

    【讨论】:

    • 感谢@RoadRunner,这也是一个有用的解决方案。
    猜你喜欢
    • 2014-06-29
    • 1970-01-01
    • 2015-04-24
    • 2012-12-08
    • 2011-10-12
    • 1970-01-01
    • 2016-09-01
    • 2021-03-20
    • 2016-12-07
    相关资源
    最近更新 更多