問題描述
在使用Azure的存儲(chǔ)服務(wù)時(shí)候,如果上傳的文件大于了100MB, 1GB的情況下,如何上傳呢?
問題解答
使用Azure存儲(chǔ)服務(wù)時(shí),如果要上傳文件到Azure Blob,有很多種工具可以實(shí)現(xiàn)。如:Azure 門戶, Azure Storage Explorer, 命令行工具 az copy等。
如果使用SDK,通過自定義代碼上傳的時(shí),需要主要大文件上傳時(shí)候需要考慮的問題。 Azure Blob支持兩種上傳方式:整體上傳和分塊上傳。
整塊上傳:當(dāng)上傳到塊 Blob 的文件小于等于 **SingleBlobUploadThresholdInBytes **屬性(客戶端可以通過設(shè)置該屬性設(shè)置單個(gè) Blob 上傳的最大值,范圍介于 1MB 和 256MB 之間)的值時(shí),則可以采用整體上傳的方式。
分塊上傳:當(dāng)上傳的塊 Blob 的文件大于 SingleBlobUploadThresholdInBytes 屬性的值時(shí),存儲(chǔ)客戶端會(huì)根據(jù) StreamWriteSizeInBytes (客戶端可以通過設(shè)置該屬性設(shè)置單個(gè)分塊 Blob 的大小,范圍介于 16KB 和 100MB 之間) 的值將文件分解成塊, 采用分塊上傳的方式上傳文件。
如下示例,就是使用.NET7.0創(chuàng)建的示例代碼:
1) 在VS Code中,使用 dotnet new console 創(chuàng)建一個(gè)空的控制臺(tái)項(xiàng)目
dotnet new console --framework net7.0
2)添加 Microsoft.WindowsAzure.Storage 引用
dotnet add package WindowsAzure.Storage --version 9.3.3
4)修改 Program.cs 代碼
// See https://aka.ms/new-console-template for more information
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.RetryPolicies;
Console.WriteLine("Hello, World!");
TimeSpan backOffPeriod = TimeSpan.FromSeconds(2);
int retryCount = 1;
//設(shè)置請(qǐng)求選項(xiàng)
BlobRequestOptions requestoptions = new BlobRequestOptions()
{
SingleBlobUploadThresholdInBytes = 1024 * 1024 * 10, //10MB
ParallelOperationThreadCount = 12,
RetryPolicy = new ExponentialRetry(backOffPeriod, retryCount),
};
//String storageConnectionString = System.Environment.GetEnvironmentVariable("StorageConnectionString", EnvironmentVariableTarget.User);
//Console.WriteLine("String account string : "+storageConnectionString);
String storageConnectionString ="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobclient = account.CreateCloudBlobClient();
//設(shè)置客戶端默認(rèn)請(qǐng)求選項(xiàng)
blobclient.DefaultRequestOptions = requestoptions;
CloudBlobContainer blobcontainer = blobclient.GetContainerReference("uploadfiles-123");
await blobcontainer.CreateIfNotExistsAsync();
//文件路徑,文件大小 117MB
string sourcePath = @"C:\WorkSpace\ETW\1.20211017.060119_000001.etl";
CloudBlockBlob blockblob = blobcontainer.GetBlockBlobReference("bigfiles");
//設(shè)置單個(gè)塊 Blob 的大?。ǚ謮K方式)
blockblob.StreamWriteSizeInBytes = 1024 * 1024 * 5;
try
{
Console.WriteLine("uploading");
//使用 Stopwatch 查看上傳時(shí)間
var timer = System.Diagnostics.Stopwatch.StartNew();
using (var filestream = System.IO.File.OpenRead(sourcePath))
{
await blockblob.UploadFromStreamAsync(filestream);
}
timer.Stop();
Console.WriteLine(timer.ElapsedMilliseconds);
Console.WriteLine("Upload Successful, Time:" + timer.ElapsedMilliseconds);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
-
代碼完成。如果在Azure存儲(chǔ)賬號(hào)中開啟了診斷日志,當(dāng)上傳大文件后,就可以通過日志分析出,以上代碼執(zhí)行了多次Upload操作以完成大文件的上傳!
image
參考資料
上傳大文件到 Azure 存儲(chǔ)塊 Blob:https://docs.azure.cn/zh-cn/articles/azure-operations-guide/storage/aog-storage-blob-howto-upload-big-file-to-storage
WindowsAzure.Storage : https://www.nuget.org/packages/WindowsAzure.Storage/
Tutorial: Create a .NET console application using Visual Studio Code : https://learn.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code?pivots=dotnet-7-0
當(dāng)在復(fù)雜的環(huán)境中面臨問題,格物之道需:濁而靜之徐清,安以動(dòng)之徐生。 云中,恰是如此!
標(biāo)簽: Storage Blob Upload, Microsoft.WindowsAzure.Storage, UploadFromStreamAsync
