【问题标题】:Azure automation, PowerShell to fetch a file in private blob containerAzure 自动化,PowerShell 来获取私有 blob 容器中的文件
【发布时间】:2020-11-16 20:17:42
【问题描述】:

我有一个 azure blob 容器设置为私有。我想使用 PowerShell 下载此容器中的文件。

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

Invoke-WebRequest -Uri $uri -OutFile $filePath

【问题讨论】:

    标签: powershell azure


    【解决方案1】:

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

    【讨论】:

      【解决方案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
      

      【讨论】:

        【解决方案3】:
        $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 
        

        【讨论】:

          猜你喜欢
          • 2022-10-07
          • 2020-09-05
          • 2020-06-25
          • 2017-07-27
          • 2020-09-03
          • 2020-01-12
          • 2017-11-16
          • 2014-06-22
          相关资源
          最近更新 更多