從 Azure 儲存體下載大量隨機資料
本教學課程是一個系列課程的第三部分。 本教學課程說明如何從 Azure 儲存體下載大量資料。
在系列的第三部分中,您將了解如何:
- 更新應用程式
- 執行應用程式
- 驗證連線數目
必要條件
若要完成本教學課程,您必須先完成上一個儲存體教學課程:平行上傳大量隨機資料到 Azure 儲存體。
遠端連線到您的虛擬機器
若要與虛擬機器建立遠端桌面工作階段,請在您的本機電腦上使用下列命令。 以虛擬機器的公用 IP 位址取代 IP 位址。 出現提示時,請輸入您在建立虛擬機器時所使用的認證。
mstsc /v:<publicIpAddress>
更新應用程式
在上一個教學課程中,您只將檔案上傳到儲存體帳戶。 在文字編輯器中開啟 D:\git\storage-dotnet-perf-scale-app\Program.cs
。 使用下列範例取代 Main
方法。 此範例已經將上傳工作變更為註解,並將下載工作及完成後刪除儲存體帳戶中內容的工作取消註解。
public static void Main(string[] args)
{
Console.WriteLine("Azure Blob storage performance and scalability sample");
// Set threading and default connection limit to 100 to
// ensure multiple threads and connections can be opened.
// This is in addition to parallelism with the storage
// client library that is defined in the functions below.
ThreadPool.SetMinThreads(100, 4);
ServicePointManager.DefaultConnectionLimit = 100; // (Or More)
bool exception = false;
try
{
// Call the UploadFilesAsync function.
// await UploadFilesAsync();
// Uncomment the following line to enable downloading of files from the storage account.
// This is commented out initially to support the tutorial at
// https://learn.microsoft.com/azure/storage/blobs/storage-blob-scalable-app-download-files
await DownloadFilesAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
exception = true;
}
finally
{
// The following function will delete the container and all files contained in them.
// This is commented out initially as the tutorial at
// https://learn.microsoft.com/azure/storage/blobs/storage-blob-scalable-app-download-files
// has you upload only for one tutorial and download for the other.
if (!exception)
{
// await DeleteExistingContainersAsync();
}
Console.WriteLine("Press any key to exit the application");
Console.ReadKey();
}
}
應用程式更新之後,您需要再次建置該應用程式。 開啟 Command Prompt
並瀏覽至 D:\git\storage-dotnet-perf-scale-app
。 執行下列範例中的 dotnet build
,以重建應用程式:
dotnet build
執行應用程式
應用程式重建完成之後,即能以更新過的程式碼執行應用程式。 請開啟 Command Prompt
(若未開啟) 並瀏覽至 D:\git\storage-dotnet-perf-scale-app
。
輸入 dotnet run
執行應用程式。
dotnet run
DownloadFilesAsync
工作如下列範例所示:
應用程式會讀取 storageconnectionstring中指定之儲存體帳戶內的容器。 其會使用 GetBlobs 方法來逐一查看 Blob,並使用 DownloadToAsync 方法將 Blob 下載到本機電腦。
private static async Task DownloadFilesAsync()
{
BlobServiceClient blobServiceClient = GetBlobServiceClient();
// Path to the directory to upload
string downloadPath = Directory.GetCurrentDirectory() + "\\download\\";
Directory.CreateDirectory(downloadPath);
Console.WriteLine($"Created directory {downloadPath}");
// Specify the StorageTransferOptions
var options = 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
};
List<BlobContainerClient> containers = new List<BlobContainerClient>();
foreach (BlobContainerItem container in blobServiceClient.GetBlobContainers())
{
containers.Add(blobServiceClient.GetBlobContainerClient(container.Name));
}
// Start a timer to measure how long it takes to download all the files.
Stopwatch timer = Stopwatch.StartNew();
// Download the blobs
try
{
int count = 0;
// Create a queue of tasks that will each upload one file.
var tasks = new Queue<Task<Response>>();
foreach (BlobContainerClient container in containers)
{
// Iterate through the files
foreach (BlobItem blobItem in container.GetBlobs())
{
string fileName = downloadPath + blobItem.Name;
Console.WriteLine($"Downloading {blobItem.Name} to {downloadPath}");
BlobClient blob = container.GetBlobClient(blobItem.Name);
// Add the download task to the queue
tasks.Enqueue(blob.DownloadToAsync(fileName, default, options));
count++;
}
}
// Run all the tasks asynchronously.
await Task.WhenAll(tasks);
// Report the elapsed time.
timer.Stop();
Console.WriteLine($"Downloaded {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}");
}
}
驗證連線
當檔案正在下載時,您可以確認與您儲存體帳戶建立的並行連線的數目。 開啟主控台視窗,然後輸入 netstat -a | find /c "blob:https"
。 此命令可顯示目前開啟的連線數目。 如下列範例中所見,從儲存體帳戶下載檔案時開啟了超過 280 個連線。
C:\>netstat -a | find /c "blob:https"
289
C:\>
下一步
在此系列的第三個部分中,您已學到從儲存體帳戶下載大量資料,包括如何:
- 執行應用程式
- 驗證連線數目
前往到系列的第四部分,以在入口網站中驗證輸送量和延遲計量。