【问题标题】:azure storage / c#: Download all files from a directory in a containerazure storage / c#:从容器中的目录下载所有文件
【发布时间】:2019-04-26 08:16:03
【问题描述】:

有没有一种简单的方法可以从容器内部下载所有文件并维护目录结构?

例如,我已将以下目录上传到容器“用户”中的 Azure 存储帐户。

../用户名/文件/ ../用户名/文件/default.htm ../用户名/文件/1.txt ../用户名/文件/2.txt

下面的代码会将d​​efault.htm文件下载到C:\users\username\files\default.htm

问题是我不想下载特定文件。我希望能够获取 users/username/ 中的每个文件,并让它在所需的本地存储路径中输出相同的目录结构。

我将在这个容器内有多个“目录”(不同的用户),它们的结构并不总是相同的,有些可能有多个带有文件的子目录,有些可能没有,等等。所以我真想找个说法:

从“用户/用户名”下载所有到 C:\users\username\

private static void DownloadFromAzureStorage()// RUN on new server
{
    string azureStorageAccountName = 
    ConfigurationManager.AppSettings["AzureStorageAccountName"];
    string azureStorageAccountKey = 
    ConfigurationManager.AppSettings["AzureStorageAccountKey"];
    string target = 
    ConfigurationManager.AppSettings["TargetDirectoryPath"];
    //C:\username\files\
    var storageCredentials = new 
    StorageCredentials(azureStorageAccountName, azureStorageAccountKey);
    var csa = new CloudStorageAccount(storageCredentials, true);

    CloudBlobClient blobClient = csa.CreateCloudBlobClient();
    CloudBlobContainer container = 
    blobClient.GetContainerReference("users");

    CloudBlockBlob blockBlob = 
    container.GetBlockBlobReference("username/files/default.htm");
    string path = (target + "default.htm");
    blockBlob.DownloadToFile(path, FileMode.OpenOrCreate);
}

【问题讨论】:

    标签: c# azure-storage azure-blob-storage


    【解决方案1】:

    blob 名称包括容器中文件的完整路径,因此如果您使用 CloudBlockBlob Name 属性保存 blob,您将获得与容器中相同的文件夹结构。

    这样试试

    var blobContainerName = "users";
    var storageCredentials = new StorageCredentials(azureStorageAccountName, azureStorageAccountKey);
    var storageAccount = new CloudStorageAccount(storageCredentials, true);
    var context = new OperationContext();
    var options = new BlobRequestOptions();
    var cloudBlobClient = storageAccount.CreateCloudBlobClient();
    var cloudBlobContainer = cloudBlobClient.GetContainerReference(blobContainerName);
    BlobContinuationToken blobContinuationToken = null;
    do
    {
        var results = await cloudBlobContainer.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All,
            null, blobContinuationToken, options, context);
        blobContinuationToken = results.ContinuationToken;
        foreach (var item in results.Results)
        {
            if(item is CloudBlockBlob blockBlob)
            {
                string path = $"{target}{blockBlob.Name}";
                blockBlob.DownloadToFile(path, FileMode.OpenOrCreate);
            }
        }
    } while (blobContinuationToken != null);
    

    【讨论】:

      【解决方案2】:

      您可能想尝试AzCopy 命令行工具,它能够递归下载容器/目录:

      AzCopy /Source:https://myaccount.blob.core.windows.net/users /Dest:C:\users /SourceKey:key /S
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-11
        • 1970-01-01
        • 1970-01-01
        • 2015-05-18
        • 2011-07-10
        • 2021-05-04
        相关资源
        最近更新 更多