Azure自动化,PowerShell用于获取私有blob容器中的文件

时间:2016-01-20 14:49:02

标签: powershell azure

我将azure blob容器设置为private。我想使用PowerShell在此容器中下载文件。

这就是我所说的,但它每次都给我ResourceNotFound错误。即使我把-Credential和我的用户名/访问密钥。当我将容器切换到公共访问时,它总是有效。我错过了什么吗?

rejection

3 个答案:

答案 0 :(得分:2)

使用Invoke-WebRequest类似于在浏览器中打开链接。这是从Azure存储下载文件的合法方式,但要做到这一点,您需要包含SAS (Shared Access Signature)的URI,您必须在代码中使用它之前生成该enter image description here。实现这一目标的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

答案 1 :(得分:2)

我最终通过Azure PowerShell Az module解决了类似的要求,

$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

答案 2 :(得分:0)

$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