2017-07-19 55 views
0

我需要能够使用Azure存储REST API从blob存储帐户下载文件夹及其内容。Azure存储API:下载整个文件夹/前缀/目录

我创建了一个函数(New-StorageAccountAthorisationHeader),该函数创建可以下载单个文件的(身份验证)标头,但我无法找到任何有关如何下载整个文件夹的参考。

如果我将该文件夹作为$blob参数传递,我会得到一个BlobNotFound错误。

该文件夹的URL是:https://mystorageaccount.blob.core.windows.net/acontainer/somefolder。 “somefolder” 的内容是这样的:

Folder1 
    FolderA 
    FileA.txt 
    FolderB 
    FileB.txt 
    FileC.txt 

新StorageAccountAthorisationHeader:

function New-StorageAccountAuthorizationHeader 
{ 
    [cmdletbinding()] 
    param 
    (
     [string]$StorageAccountName, 
     [string]$Container, 
     [string]$Blob, 
     [string]$accesskey , 
     [string]$ResourceUri, 
     [string]$xmsversion = "2017-04-17" 
    ) 

    $xmsdate = Get-Date 
    $xmsdate = $xmsdate.ToUniversalTime() 
    $xmsdate = $xmsdate.toString('r') 

    function GetRestApiParameters 
    { 
     [cmdletbinding()] 
     param 
     (
      [Parameter(Mandatory=$true)] 
      [string]$Uri 
     ) 

     if($Uri.Contains("?")) 
     { 
      Write-Verbose "URI to extract REST parameters: $uri" 
      return ($Uri.Split("?")[1]).Split("&") 
     } 
    } 

    Write-Verbose "Generating string for signature encryption..." 

    $partUrl = "/$StorageAccountName/" 

    if($Container) 
    { 
     $partUrl = $partUrl + "$Container/" 
    } 

    if($Blob) 
    { 
     $parturl = $partUrl + "$Blob" 
    } 

######Don't change the line count or indentation of the here-string##### 
$hereString = @" 
GET 











x-ms-date:$xmsdate 
x-ms-version:$xmsversion 
$partUrl 
"@ 


    $hereString =$hereString -replace "$([char]13)$([char]10)","$([char]10)" #Change `r`n to just `n 

    $empty = $oSignature = New-Object System.Text.StringBuilder 
    $empty = $oSignature.Append($hereString) 

    Write-Verbose "Appending parameters from URI into authorisation string..." 

    $restParameters = GetRestApiParameters -Uri $ResourceUri -Verbose 

    if ($restParameters -ne $null) 
    { 
     foreach ($param in $restParameters) 
     { 
      $empty = $oSignature.Append("$([char]10)$($param.Replace('=',':'))") 
     } 
    } 

    #$oSignature.toString() 

    Write-Verbose "Encrypting string..." 
    $hmacsha = New-Object System.Security.Cryptography.HMACSHA256 
    $hmacsha.key = [Convert]::FromBase64String($accesskey) 
    $signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($oSignature.ToString())) 
    $signature = [Convert]::ToBase64String($signature) 

    Write-Verbose "Building header..." 
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" 
    $headers.Add("x-ms-version", $xmsversion) 
    $headers.Add("x-ms-date", $xmsdate) 
    $headers.Add("Authorization", "SharedKey " + $StorageAccountName + ":" + $signature) 
    #$headers.Add("x-ms-blob-type","BlockBlob") 
    #$headers.Add("Content-Type", "application\xml") 

    Write-Verbose ("Header: $($headers | Out-String)") 

    Return $headers 
} 

而且我会叫它:

$StorageAccountName = "mystorageaccount" 
$container = "acontainer" 
$blob = "somefile.txt" 

$uriToDownloadBlobs = "https://" + $StorageAccountName + ".blob.core.windows.net/$container/$blob" 

$header = $null 
$header = New-StorageAccountAuthorizationHeader -StorageAccountName $StorageAccountName -ResourceUri $uriToDownloadBlobs -Verbose -Container $container -Blob $blob 

$result = Invoke-WebRequest -Headers $header -Uri $uriToDownloadBlobs -OutFile C:\Temp\$blob -PassThru 

$result 

所以这个工作,但正如我所说,我在任何提示后帮助下载整个文件夹。

回答

0

看起来这是不可能的?尽管我有兴趣了解Azure存储浏览器如何实现它。

我的解决方案是压缩文件,然后使用上面的下载单个ZIP文件。在任何一端压缩和提取一些额外的代码行,但这是当时最快的方式,它适用于VSTS任务。

相关问题