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:usersusernamefilesdefault.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:usersusername
private static void DownloadFromAzureStorage()// RUN on new server
{
string azureStorageAccountName =
ConfigurationManager.AppSettings["AzureStorageAccountName"];
string azureStorageAccountKey =
ConfigurationManager.AppSettings["AzureStorageAccountKey"];
string target =
ConfigurationManager.AppSettings["TargetDirectoryPath"];
//C:usernamefiles
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-storage-blobs
add a comment |
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:usersusernamefilesdefault.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:usersusername
private static void DownloadFromAzureStorage()// RUN on new server
{
string azureStorageAccountName =
ConfigurationManager.AppSettings["AzureStorageAccountName"];
string azureStorageAccountKey =
ConfigurationManager.AppSettings["AzureStorageAccountKey"];
string target =
ConfigurationManager.AppSettings["TargetDirectoryPath"];
//C:usernamefiles
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-storage-blobs
add a comment |
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:usersusernamefilesdefault.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:usersusername
private static void DownloadFromAzureStorage()// RUN on new server
{
string azureStorageAccountName =
ConfigurationManager.AppSettings["AzureStorageAccountName"];
string azureStorageAccountKey =
ConfigurationManager.AppSettings["AzureStorageAccountKey"];
string target =
ConfigurationManager.AppSettings["TargetDirectoryPath"];
//C:usernamefiles
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-storage-blobs
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:usersusernamefilesdefault.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:usersusername
private static void DownloadFromAzureStorage()// RUN on new server
{
string azureStorageAccountName =
ConfigurationManager.AppSettings["AzureStorageAccountName"];
string azureStorageAccountKey =
ConfigurationManager.AppSettings["AzureStorageAccountKey"];
string target =
ConfigurationManager.AppSettings["TargetDirectoryPath"];
//C:usernamefiles
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-storage-blobs
c# azure-storage azure-storage-blobs
asked Nov 23 '18 at 18:19
bacis09bacis09
274
274
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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);
add a comment |
You may want to try AzCopy command line tool which is able to download a container/directory recursively:
AzCopy /Source:https://myaccount.blob.core.windows.net/users /Dest:C:users /SourceKey:key /S
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451403%2fazure-storage-c-download-all-files-from-a-directory-in-a-container%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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);
add a comment |
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);
add a comment |
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);
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);
edited Nov 23 '18 at 19:50
answered Nov 23 '18 at 19:43
Marcus HöglundMarcus Höglund
9,73862545
9,73862545
add a comment |
add a comment |
You may want to try AzCopy command line tool which is able to download a container/directory recursively:
AzCopy /Source:https://myaccount.blob.core.windows.net/users /Dest:C:users /SourceKey:key /S
add a comment |
You may want to try AzCopy command line tool which is able to download a container/directory recursively:
AzCopy /Source:https://myaccount.blob.core.windows.net/users /Dest:C:users /SourceKey:key /S
add a comment |
You may want to try AzCopy command line tool which is able to download a container/directory recursively:
AzCopy /Source:https://myaccount.blob.core.windows.net/users /Dest:C:users /SourceKey:key /S
You may want to try AzCopy command line tool which is able to download a container/directory recursively:
AzCopy /Source:https://myaccount.blob.core.windows.net/users /Dest:C:users /SourceKey:key /S
answered Nov 24 '18 at 4:50
Zhaoxing Lu - MicrosoftZhaoxing Lu - Microsoft
3,418621
3,418621
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451403%2fazure-storage-c-download-all-files-from-a-directory-in-a-container%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown