1

【Azure 存储服务】.NET7.0 示例代码之上传大文件到Azure Storage Blob - 路边两盏灯

 1 year ago
source link: https://www.cnblogs.com/lulight/p/17061631.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【Azure 存储服务】.NET7.0 示例代码之上传大文件到Azure Storage Blob

在使用Azure的存储服务时候,如果上传的文件大于了100MB, 1GB的情况下,如何上传呢?

使用Azure存储服务时,如果要上传文件到Azure Blob,有很多种工具可以实现。如:Azure 门户, Azure Storage Explorer, 命令行工具 az copy等。

如果使用SDK,通过自定义代码上传的时,需要主要大文件上传时候需要考虑的问题。 Azure Blob支持两种上传方式:整体上传和分块上传

  • 整块上传:当上传到块 Blob 的文件小于等于 SingleBlobUploadThresholdInBytes 属性(客户端可以通过设置该属性设置单个 Blob 上传的最大值,范围介于 1MB 和 256MB 之间)的值时,则可以采用整体上传的方式。

  • 分块上传:当上传的块 Blob 的文件大于 SingleBlobUploadThresholdInBytes 属性的值时,存储客户端会根据 StreamWriteSizeInBytes (客户端可以通过设置该属性设置单个分块 Blob 的大小,范围介于 16KB 和 100MB 之间) 的值将文件分解成块, 采用分块上传的方式上传文件。

如下示例,就是使用.NET7.0创建的示例代码:

1) 在VS Code中,使用 dotnet new console 创建一个空的控制台项目

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;
//设置请求选项
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();
//设置客户端默认请求选项
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");
//设置单个块 Blob 的大小(分块方式)
blockblob.StreamWriteSizeInBytes = 1024 * 1024 * 5;
try
{
    Console.WriteLine("uploading");
    //使用 Stopwatch 查看上传时间
    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);
}

5) 代码完成。如果在Azure存储账号中开启了诊断日志,当上传大文件后,就可以通过日志分析出,以上代码执行了多次Upload操作以完成大文件的上传!

2127802-20230119152553562-2040619549.png

上传大文件到 Azure 存储块 Blob:https://docs.azure.cn/zh-cn/articles/azure-operations-guide/storage/aog-storage-blob-howto-upload-big-file-to-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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK