如何将文件夹上传至Azure存储

4

我希望能够上传整个文件夹到Azure存储中。 我知道可以使用以下方式上传单个文件:

blobReference.UploadFromFile(fileName);

但是我找不到上传整个文件夹(递归)的方法。有这样的方法吗?或者有示例代码吗?

谢谢


1
我认为没有这样的方法,因为在Azure上有一个扁平的层次结构,其中所有内容都是容器中的Blob。您需要遍历每个文件并上传它。 - Sajal
2
Azure支持Blob容器中的子文件夹 - 不是真的。子文件夹只是Blob名称的前缀。您无法在Azure Blob存储中创建空的子文件夹。 - Gaurav Mantri
快速搜索谷歌会得到以下结果:http://www.dotnetspeak.com/azure/uploading-directory-to-azure-blob-storage/ - Simon
Gaurav,Azure对前缀有足够的支持,以至于它们实际上是子文件夹。他们自己的浏览器工具将它们显示为这样。 - Simon
1
@Simon - 一点也不吹毛求疵。根本就没有“子文件夹”。试着枚举一个包含100,000个项目的容器中的内容,其中有一些在“子文件夹”中(其实并不是子文件夹)。在搜索正确的项目时,您仍将枚举所有100,000个项目(因为它必须搜索前缀名称)。另外,FYI Gaurav编写了最早和最流行的Azure存储资源管理器工具之一-他知道自己在说什么。 - David Makogon
显示剩余2条评论
4个回答

13

文件夹结构可以简单地成为文件名的一部分:

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

如果您按照此示例上传,文件将出现在“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);
  }

有什么办法可以相反地做吗?从存储中下载一个文件夹...? - Dafna

1
您可以尝试使用Microsoft Azure Storage DataMovement Library,它支持高性能、可扩展和可靠的传输Blob目录。此外,在传输过程中,它还支持取消并恢复传输。这里是将文件夹上传到Azure Blob存储的示例。

1

1
命令行没有批量上传多个文件的选项。但是,您可以使用find或循环来上传多个文件,例如:
#!/bin/bash

export AZURE_STORAGE_ACCOUNT='your_account'
export AZURE_STORAGE_ACCESS_KEY='your_access_key'

export container_name='name_of_the_container_to_create'
export source_folder=~/path_to_local_file_to_upload/*


echo "Creating the container..."
azure storage container create $container_name

for f in $source_folder
do
  echo "Uploading $f file..."
  azure storage blob upload $f $container_name $(basename $f)
  cat $f
done

echo "Listing the blobs..."
azure storage blob list $container_name

echo "Done"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接