我希望能够上传整个文件夹到Azure存储中。 我知道可以使用以下方式上传单个文件:
blobReference.UploadFromFile(fileName);
但是我找不到上传整个文件夹(递归)的方法。有这样的方法吗?或者有示例代码吗?
谢谢
我希望能够上传整个文件夹到Azure存储中。 我知道可以使用以下方式上传单个文件:
blobReference.UploadFromFile(fileName);
但是我找不到上传整个文件夹(递归)的方法。有这样的方法吗?或者有示例代码吗?
谢谢
文件夹结构可以简单地成为文件名的一部分:
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);
}
从CLI中,您可以使用以下命令:
az storage blob upload-batch
#!/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"