Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload a folder to Azure storage

I would like to upload an entire folder to azure storage. I know I can upload a file using:

blobReference.UploadFromFile(fileName);

But could not find a method to upload an entire folder (recursively). Is there such method? Or maybe an example code?

Thanks

like image 522
Dafna Avatar asked Dec 04 '22 22:12

Dafna


1 Answers

The folder structure can simply be part of the filename:

string myfolder = "datadir";
string myfilename = "mydatafile";
string fileName = String.Format("{0}/{1}.csv", myfolder, myfilename);
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);

If you upload like this example, files would appear in the container in the 'datadir' folder.

That means that you can use this to copy a directory structure to upload:

foreach (string file in Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)) {
    // file would look like "C:\dir1\dir2\blah.txt"

    // Don't know if this is the prettiest way, but it will work:
    string cloudfilename = file.Substring(3).Replace('\\', '/');

    // get the blob reference and push the file contents to it:
    CloudBlockBlob blob = container.GetBlockBlobReference(cloudfileName);
    blob.UploadFromFile(file);
  }
like image 135
Niels Avatar answered Dec 14 '22 21:12

Niels