Azure PowerShell 从容器中下载 blob 内容

Azure PowerShell Download blob contents from container

我正在尝试从 Azure 存储帐户下载 sme blob 文件。

父容器中混合了其他容器和 blockblob,我只需要下载 blockblob 而不是其他容器,我找不到将它们分开的方法,我还需要下载一些来自容器内容器的 blob。

我的代码将下载父 blob 中的所有内容,包括所有子容器。

  $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

父 blob 中有大约 75 个文件,data\downloads 中有 123 个文件。

你能不能只 运行 以下内容并将其限制为 BlockBlobs?

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

使用较新的 Azure PowerShell Az module, you can use Get-AzStorageBlob to list all the block blobs from the container, then use Get-AzStorageBlobContent 下载 blob。

, we can use Where-Object 或其别名 ? 所示,用于过滤块 blob 类型。

演示:

$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