首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure存储API:下载整个文件夹/前缀/目录

Azure存储API:下载整个文件夹/前缀/目录
EN

Stack Overflow用户
提问于 2017-07-20 03:00:16
回答 1查看 528关注 0票数 1

我需要能够仅使用Azure storage REST API从blob存储帐户下载文件夹及其内容。

我已经创建了一个函数(New-StorageAccountAthorisationHeader),它创建了我可以下载单个文件的(身份验证)头,但我找不到任何关于如何下载整个文件夹的参考。

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

该文件夹的网址为:https://mystorageaccount.blob.core.windows.net/acontainer/somefolder。“某个文件夹”的内容如下所示:

代码语言:javascript
复制
Folder1
  FolderA
     FileA.txt
  FolderB
    FileB.txt
    FileC.txt

New-StorageAccountAthorisationHeader:

代码语言:javascript
复制
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
}

我把它叫做:

代码语言:javascript
复制
$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

因此,这是可行的,但正如我所说,我正在寻找任何提示,以帮助下载整个文件夹。

EN

回答 1

Stack Overflow用户

发布于 2017-07-28 19:51:34

看起来这是不可能的?尽管我很有兴趣看看它是如何使用Azure Storage Explorer这样的工具完成的。

我的解决方案是将文件压缩,然后使用上面的代码下载单个zip文件。两端都需要几行额外的代码来压缩和提取,但这是当时最快的方法,并且它可以很好地处理VSTS任务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45198981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档