將大量隨機資料平行上傳至 Azure 儲存體

本教學課程是一個系列的第二部分。 本教學課程會示範如何部署應用程式,來將大量的隨機資料上傳至 Azure 儲存體帳戶。

在本系列的第二部分中,您將瞭解如何:

  • 設定連接字串
  • 建置應用程式
  • 執行應用程式
  • 驗證連線數目

Microsoft Azure Blob 儲存體提供可調整服務來儲存您的資料。 若要確保您的應用程式能發揮最好的效能,建議您了解 Blob 儲存體的運作方式。 關於 Azure Blob 限制的知識十分重要,若要深入了解這些限制,請造訪:Blob 儲存體的可擴縮性和效能目標

使用 Blob 設計高效能的應用程式時,分割區命名是另一個可能很重要的要素。 對於大於或等於 4 MiB 的區塊大小,則會使用高輸送量的區塊 Blob,且分割區命名並不會影響效能。 對於小於 4 MiB 的區塊大小,Azure 儲存體使用範圍型的資料分割配置來進行縮放和負載平衡。 此設定表示具有相似命名慣例或前置詞的檔案會進入相同分割區。 此邏輯也包含將對其上傳檔案的容器名稱。 在本教學課程中,您會使用以 GUID 命名的檔案和隨機產生的內容。 然後這些項目會上傳至五個具有隨機名稱的不同容器。

必要條件

若要完成本教學課程,您必須先完成上一個儲存體教學課程:為可擴充的應用程式建立虛擬機器和儲存體帳戶

遠端連線到您的虛擬機器

在本機電腦上使用下列命令,建立使用虛擬機器的遠端桌面工作階段。 以虛擬機器的公用 IP 位址取代 IP 位址。 出現提示時,請輸入您在建立虛擬機器時所使用的認證。

mstsc /v:<publicIpAddress>

設定連接字串

在 Azure 入口網站中,瀏覽至您的儲存體帳戶。 在儲存體帳戶中選取 [設定] 下的 [存取金鑰]。 從主要或次要金鑰複製連接字串。 登入先前教學課程中所建立的虛擬機器。 以系統管理員身分開啟 [命令提示字元],並以 /m 執行 setx 命令,此命令會儲存機器的設定環境變數。 您須重新載入命令提示字元,才能使用環境變數。 取代下列範例中的 <storageConnectionString>

setx storageconnectionstring "<storageConnectionString>" /m

完成後,請開啟另一個命令提示字元,並瀏覽至 D:\git\storage-dotnet-perf-scale-app,然後輸入 dotnet build 來建置應用程式。

執行應用程式

瀏覽至 D:\git\storage-dotnet-perf-scale-app

輸入 dotnet run 執行應用程式。 第一次執行 dotnet 時,它會填入您的本機套件快取,以提升還原速度及啟用離線存取。 完成此命令需一分鐘的時間,且只會發生一次。

dotnet run

應用程式會建立五個隨機命名的容器,並開始將暫存目錄中的檔案上傳至儲存體帳戶。

UploadFilesAsync 方法如下列範例所示:

private static async Task UploadFilesAsync()
{
    // Create five randomly named containers to store the uploaded files.
    BlobContainerClient[] containers = await GetRandomContainersAsync();

    // Path to the directory to upload
    string uploadPath = Directory.GetCurrentDirectory() + "\\upload";

    // Start a timer to measure how long it takes to upload all the files.
    Stopwatch timer = Stopwatch.StartNew();

    try
    {
        Console.WriteLine($"Iterating in directory: {uploadPath}");
        int count = 0;

        Console.WriteLine($"Found {Directory.GetFiles(uploadPath).Length} file(s)");

        // Specify the StorageTransferOptions
        BlobUploadOptions options = new BlobUploadOptions
        {
            TransferOptions = new StorageTransferOptions
            {
                // Set the maximum number of workers that 
                // may be used in a parallel transfer.
                MaximumConcurrency = 8,

                // Set the maximum length of a transfer to 50MB.
                MaximumTransferSize = 50 * 1024 * 1024
            }
        };

        // Create a queue of tasks that will each upload one file.
        var tasks = new Queue<Task<Response<BlobContentInfo>>>();

        // Iterate through the files
        foreach (string filePath in Directory.GetFiles(uploadPath))
        {
            BlobContainerClient container = containers[count % 5];
            string fileName = Path.GetFileName(filePath);
            Console.WriteLine($"Uploading {fileName} to container {container.Name}");
            BlobClient blob = container.GetBlobClient(fileName);

            // Add the upload task to the queue
            tasks.Enqueue(blob.UploadAsync(filePath, options));
            count++;
        }

        // Run all the tasks asynchronously.
        await Task.WhenAll(tasks);

        timer.Stop();
        Console.WriteLine($"Uploaded {count} files in {timer.Elapsed.TotalSeconds} seconds");
    }
    catch (RequestFailedException ex)
    {
        Console.WriteLine($"Azure request failed: {ex.Message}");
    }
    catch (DirectoryNotFoundException ex)
    {
        Console.WriteLine($"Error parsing files in the directory: {ex.Message}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Exception: {ex.Message}");
    }
}

下列範例是 Windows 系統上執行的應用程式輸出 (已截斷)。

Created container 2dbb45f4-099e-49eb-880c-5b02ebac135e
Created container 0d784365-3bdf-4ef2-b2b2-c17b6480792b
Created container 42ac67f2-a316-49c9-8fdb-860fb32845d7
Created container f0357772-cb04-45c3-b6ad-ff9b7a5ee467
Created container 92480da9-f695-4a42-abe8-fb35e71eb887
Iterating in directory: C:\git\myapp\upload
Found 5 file(s)
Uploading 1d596d16-f6de-4c4c-8058-50ebd8141e4d.pdf to container 2dbb45f4-099e-49eb-880c-5b02ebac135e
Uploading 242ff392-78be-41fb-b9d4-aee8152a6279.pdf to container 0d784365-3bdf-4ef2-b2b2-c17b6480792b
Uploading 38d4d7e2-acb4-4efc-ba39-f9611d0d55ef.pdf to container 42ac67f2-a316-49c9-8fdb-860fb32845d7
Uploading 45930d63-b0d0-425f-a766-cda27ff00d32.pdf to container f0357772-cb04-45c3-b6ad-ff9b7a5ee467
Uploading 5129b385-5781-43be-8bac-e2fbb7d2bd82.pdf to container 92480da9-f695-4a42-abe8-fb35e71eb887
Uploaded 5 files in 16.9552163 seconds

驗證連線

當檔案正在上傳時,您可以確認與您儲存體帳戶建立的並行連線數目。 開啟主控台視窗,然後輸入 netstat -a | find /c "blob:https"。 此命令可顯示目前開啟的連線數目。 如以下範例中所見,將隨機檔案上傳至儲存體帳戶時時開啟了 800 個連線。 此值會在整個上傳作業期間變更。 藉由以區塊的形式來平行上傳,就可大幅減少傳送內容所需的時間。

C:\>netstat -a | find /c "blob:https"
800

C:\>

下一步

在此系列的第二個部分中,您已學到如何將大量隨機資料平行上傳至儲存體帳戶,例如如何:

  • 設定連接字串
  • 建置應用程式
  • 執行應用程式
  • 驗證連線數目

接著請前往此系列的第三部分,從儲存體帳戶下載大量的資料。