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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With