下面的代碼將檔案上傳到 blob 存盤
// intialize BobClient
Azure.Storage.Blobs.BlobClient blobClient = new Azure.Storage.Blobs.BlobClient(
connectionString: connectionString,
blobContainerName: "mycrmfilescontainer",
blobName: "sampleBlobFileTest");
// upload the file
blobClient.Upload(filePath);
但是如何上傳所有檔案并維護其檔案夾結構呢?
我有一個site檔案夾,其中包含所有 html 檔案 影像 css 檔案夾以及網站的相關檔案。
我想將完整site 的檔案夾上傳到 blob 存盤上,請建議方法。
uj5u.com熱心網友回復:
請看下面的代碼:
using System.Threading.Tasks;
using System.IO;
using Azure.Storage.Blobs.Specialized;
namespace SO71558769
{
class Program
{
private const string connectionString = "connection-string";
private const string containerName = "container-name";
private const string directoryPath = "C:\temp\site\";
static async Task Main(string[] args)
{
var files = Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories);
for (var i=0; i<files.Length; i )
{
var file = files[i];
var blobName = file.Replace(directoryPath, "").Replace("\\", "/");
BlockBlobClient blobClient = new BlockBlobClient(connectionString, containerName, blobName);
using (var fs = File.Open(file, FileMode.Open))
{
await blobClient.UploadAsync(fs);
}
}
}
}
}
本質上,這個想法是獲取檔案夾中所有檔案的串列,然后遍歷該集合并上傳每個檔案。
要獲取 blob 名稱,您只需在檔案名中找到目錄路徑并將其替換為空字串即可獲取 blob 名稱(例如,如果完整檔案路徑為C:\temp\site\html\index.html,則 blob 名稱將為 `html\index.html) .
如果您使用的是 Windows,那么您還需要將\delimiter替換為 delimiter,/以便您獲得最終的 blob 名稱為html/index.html.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/446782.html
