以编程方式下载以前版本的 Azure 存储 Blob 内容
Download Previous Version of Azure Storage Blob Content Programmatically
是否可以使用 Powershell 或 Azure CLI(或其他)下载启用了 blob 版本控制的以前版本的 Azure 存储 Blob?
在 Azure Portal 中,启用 Blob 版本控制,您可以下载以前的版本,如下图所示:
Downloading previous version from Azure portal
使用 Powershell,我可以使用 Get-AZStorageBlob -IncludeVersion
参数
retrieve/download 以前版本的列表
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')
希望获得一些新见解 - 提前致谢。
您可以通过检查 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
如果要下载一个版本的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:\
是否可以使用 Powershell 或 Azure CLI(或其他)下载启用了 blob 版本控制的以前版本的 Azure 存储 Blob?
在 Azure Portal 中,启用 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')
希望获得一些新见解 - 提前致谢。
您可以通过检查 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
如果要下载一个版本的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:\