如何将文件夹上载到Azure存储

Daf*_*fna 1 c# azure azure-storage-blobs

我想将整个文件夹上传到azure存储.我知道我可以使用以下命令上传文件:

blobReference.UploadFromFile(fileName);
Run Code Online (Sandbox Code Playgroud)

但无法找到上传整个文件夹的方法(递归).有这样的方法吗?或者也许是一个示例代码?

谢谢

小智 9

文件夹结构可以只是文件名的一部分:

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

如果您像这个示例一样上传,文件将出现在'datadir'文件夹的容器中.

这意味着您可以使用它来复制要上载的目录结构:

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);
  }
Run Code Online (Sandbox Code Playgroud)