简体   繁体   中英

azure storage / c#: Download all files from a directory in a container

Is there an easy way to download all files and maintain the directory structure from inside of a container?

For example, I've uploaded the following directory to my Azure Storage account in container "users".

../username/files/ ../username/files/default.htm ../username/files/1.txt ../username/files/2.txt

The code below will download the default.htm file to C:\\users\\username\\files\\default.htm

The problem is I don't want to download a specific file. I want to be able to get every file inside of users/username/ and have it output the same directory structure at a desired local storage path.

I will have multiple "directories" inside of this container (different users) and their structure won't always be the same, some may have multiple sub-directories with files, some may have none, etc. So I'm really looking for a way to say :

Download ALL FROM "users/username" TO 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);
}

The blob name include the complete path to the file in the container so if you save the blobs by using the CloudBlockBlob Name property you will get the same folder structure as in the container.

Try it like this

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);

您可能要尝试使用AzCopy命令行工具,该工具可以递归下载容器/目录:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM