Azure自动化,使用PowerShell从私有Blob容器中获取文件

8
我有一个私有的Azure Blob容器。我想使用PowerShell下载此容器中的文件。
以下是我的代码,但每次都会出现ResourceNotFound错误。即使我加上-Credential和我的用户名/访问密钥。当我将容器切换为公开访问时,它总是有效的。所以我是否漏掉了什么?
Invoke-WebRequest -Uri $uri -OutFile $filePath
3个回答

7
使用 Invoke-WebRequest 类似于在浏览器中打开链接。这是从 Azure Storage 下载文件的合法方式,但要做到这一点,您需要在 URI 中包含一个 SAS(共享访问签名),您需要在使用代码之前生成它。实现此目的的 PowerShell 代码为:
#Download via URI using SAS
$BlobUri = 'https://yourstorageaccount.blob.core.windows.net/yourcontainer/yourfile.txt'
$Sas = '?sv=2015-04-05&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sr=b&sp=rw&sip=168.1.5.60-168.1.5.70&spr=https&sig=Z%2FRHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk%3D'
$OutputPath = 'C:\Temp\yourfile.txt'
$FullUri = "$BlobUri$Sas"
(New-Object System.Net.WebClient).DownloadFile($FullUri, $OutputPath)

或者,如果您已安装Azure PowerShell模块,则可以在不增加任何痛苦的情况下完成此操作:

# Download via Azure PowerShell
$StorageAccountName = 'yourstorageaccount'
$StorageAccountKey = Get-AzureStorageKey -StorageAccountName $StorageAccountName
$StorageContext = New-AzureStorageContext $StorageAccountName -StorageAccountKey $StorageAccountKey.Primary
$FileName = 'yourfile.txt'
$OutputPath = 'C:\Temp'
$ContainerName  = 'yourcontainer'
Get-AzureStorageBlobContent -Blob $FilebName -Container $ContainerName -Destination $OutputPath -Context $StorageContext

3

我最终使用Azure PowerShell Az模块解决了类似的需求,具体如下:

$BlobFilePath = 'dir\blob.file' # Relative path in blob starting from container
$OutputFilePath = 'C:\temp\blob.file' # Path to download the file to
$StorageAccountName = 'storageaccountname'
$ContainerName = 'blob-container-name'

# Prompt for Azure Account creds, if working from VM with managed identity could add also switch -Identity to use that identity directly
Connect-AzAccount
$StorageContext = New-AzStorageContext -StorageAccountName $StorageAccountName

Get-AzStorageBlobContent -Blob $BlobFilePath -Container $ContainerName -Destination $OutputFilePath -Context $StorageContext

1
$StartTime = $(get-date)
$datetime = $(get-date -f yyyy-MM-dd_hh.mm.ss)

$connection_string = ''
$AzureBlobContainerName = ''
 
$destination_path = "c:\download"


If(!(test-path $destination_path))
{
    New-Item -ItemType Directory -Force -Path $destination_path
}
$storage_account = New-AzStorageContext -ConnectionString $connection_string
 
# Download from all containers
#$containers = Get-AzStorageContainer -Context $storage_account
 
# Download from specific container
$containers = Get-AzStorageContainer -Context $storage_account | Where-Object {$_.Name -eq "$AzureBlobContainerName"}
 
$containers
Write-Host 'Starting Storage Dump...'
foreach ($container in $containers)
{
    Write-Host -NoNewline 'Processing: ' . $container.Name . '...'
    
    $blobs = Get-AzStorageBlob -Container $container.Name -Context $storage_account
    
    $container_path = $destination_path + '\' + $container.Name 
    new-item -ItemType "directory" -Path $container_path
    Write-Host -NoNewline ' Downloading files...'   
    foreach ($blob in $blobs)
    {       
        $fileNameCheck = $container_path + '\' + $blob.Name      
        if(!(Test-Path $fileNameCheck ))
        {
            Get-AzStorageBlobContent -Container $container.Name -Blob $blob.Name -Destination $container_path -Context $storage_account
        }           
    } 
    Write-Host ' Done.'
}
Write-Host 'Download complete.'

$elapsedTime = $(get-date) - $StartTime

$totalTime = "{0:HH:mm:ss}" -f ([datetime]$elapsedTime.Ticks)

Write-Output " -OK $totalTime" | Out-String 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接