2017-04-25 151 views
0

我想整個文件夾上傳到Azure存儲。 我知道我可以使用上傳文件:如何將一個文件夾上傳到Azure存儲

blobReference.UploadFromFile(fileName); 

但找不到上傳整個文件夾(遞歸)的方法。 有沒有這樣的方法?或者可能是一個示例代碼?

謝謝

+1

我不這麼認爲存在這樣的方法,由於是與一切是一個容器內的一滴湛藍一個平面層次結構。你將不得不遍歷每個文件並上傳它。 – Sajal

+0

這不是事實,Azure支持blob容器中的子文件夾。 – Simon

+2

Azure支持blob容器中的子文件夾 - 不正確。子文件夾只是blob名稱的前綴。您不能在Azure Blob存儲中創建空的子文件夾。 –

回答

5

的文件夾結構可以簡單地將文件名的一部分:

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); 
  } 
+0

完美 - 它的工作!謝謝 – Dafna

+0

任何想法如何做到相反?從存儲中下載文件夾...? – Dafna

1

命令行沒有在一次調用中批量上傳多個文件的選項。但是,您可以使用查找或循環上傳這樣的多個文件,例如:

#!/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" 
1

你可以嘗試Microsoft Azure Storage DataMovement Library支持傳輸BLOB目錄具有高性能,可擴展性和可靠性。此外,它支持取消,然後在傳輸過程中恢復。 Here是將文件夾上傳到Azure Blob存儲的示例。

相關問題