2017-07-19 6 views
0

Azure Storage REST APIのみを使用して、内容を含むフォルダをBLOBストレージアカウントからダウンロードする必要があります。AzureストレージAPI:フォルダ/プレフィックス/ディレクトリ全体をダウンロードする

私は1つのファイルをダウンロードできる(認証)ヘッダーを作成する機能を作成しましたが、フォルダ全体をダウンロードする方法についての参照は見つかりません。

$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 

だから、これは動作しますが、私が言ったように、Iフォルダ全体をダウンロードするのに役立つヒントの後に。

答えて

0

これはできないようですか?私はそれがAzure Storage Explorerのようなものでどのように行われているか見ることに興味があります。

解決方法私の解決策は、ファイルを圧縮して上記の方法を使用して単一のZIPファイルをダウンロードすることでした。コードのいくつかの余分な行を圧縮し、どちらかの端に抽出するが、それは時間の中で最も速い方法であり、VSTSタスクでうまくいきます。

関連する問題