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

简介: 【Azure 存储服务】.NET7.0 示例代码之上传大文件到Azure Storage Blob (二)

问题描述

在上一篇博文(【Azure 存储服务】.NET7.0 示例代码之上传大文件到Azure Storage Blob (一):https://www.cnblogs.com/lulight/p/17061631.html)中,介绍了第一种分片的方式上传文件。 本文章接着介绍第二种方式,使用 Microsoft.Azure.Storage.DataMovement 库中的 TransferManager.UploadAsync 通过并发的方式来上传大文件。

问题回答

第一步:添加 Microsoft.Azure.Storage.DataMovement  

dotnet add package Microsoft.Azure.Storage.DataMovement

第二步:编写示例代码

String storageConnectionString = "xxxxxxxxxxxxxxxxxxx";
        CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
        CloudBlobClient blobclient = account.CreateCloudBlobClient();
        CloudBlobContainer blobcontainer = blobclient.GetContainerReference("uploadfiles-123");
        await blobcontainer.CreateIfNotExistsAsync();
        // 获取文件路径
        string sourcePath = @"C:\home\bigfiles0120.zip";
        CloudBlockBlob docBlob = blobcontainer.GetBlockBlobReference("bigfiles-2");
        await docBlob.DeleteIfExistsAsync();
        // 设置并发操作的数量
        TransferManager.Configurations.ParallelOperations = 64;
        // 设置单块 blob 的大小,它必须在 4MB 到 100MB 之间,并且是 4MB 的倍数,默认情况下是 4MB
        TransferManager.Configurations.BlockSize = 64 * 1024 * 1024;
        // 设置传输上下文并跟踪上传进度
        var context = new SingleTransferContext();
        UploadOptions uploadOptions = new UploadOptions
        {
            DestinationAccessCondition = AccessCondition.GenerateIfExistsCondition()
        };
        context.ProgressHandler = new Progress<TransferStatus>(progress =>
        {
            //显示上传进度
            Console.WriteLine("Bytes uploaded: {0}", progress.BytesTransferred);
        });
        // 使用 Stopwatch 查看上传所需时间
        var timer = System.Diagnostics.Stopwatch.StartNew();
        // 上传 Blob
        TransferManager.UploadAsync(sourcePath, docBlob, uploadOptions, context, CancellationToken.None).Wait();
        timer.Stop();
        Console.WriteLine("Time (millisecond):" + timer.ElapsedMilliseconds);
        Console.WriteLine("upload success");

第一种分片方式上传和第二步并发上传的代码执行对比:

 

全部代码

Program.cs

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World! Start to upload big files...");
//第一种上传文件方法: Microsoft.WindowsAzure.Storage
Console.WriteLine("第一种上传文件方法: Microsoft.WindowsAzure.Storage");
await UploadMethodOne.WindowsAzureStorageUpload();
//第二种上传文件方法: Microsoft.Azure.Storage.DataMovement
Console.WriteLine("第二种上传文件方法: Microsoft.Azure.Storage.DataMovement");
await UploadMethodTwo.DataMovementUploadFiletoBlob();
Console.WriteLine("End!");

UploadMethodOne.cs

using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.RetryPolicies;
public static class UploadMethodOne
{
    public static async Task WindowsAzureStorageUpload()
    {
        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 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
        CloudBlobClient blobclient = account.CreateCloudBlobClient();
        //设置客户端默认请求选项
        blobclient.DefaultRequestOptions = requestoptions;
        CloudBlobContainer blobcontainer = blobclient.GetContainerReference("uploadfiles-123");
        await blobcontainer.CreateIfNotExistsAsync();
        //文件路径,文件大小 
        string sourcePath = @"C:\home\bigfiles0120.zip";
        CloudBlockBlob blockblob = blobcontainer.GetBlockBlobReference("bigfiles-1");
        //设置单个块 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);
        }
    }
}

 

UploadMethodTwo.cs

using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Azure.Storage.DataMovement;
public static class UploadMethodTwo
{
    public async static Task DataMovementUploadFiletoBlob()
    {
        String storageConnectionString = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
        CloudBlobClient blobclient = account.CreateCloudBlobClient();
        CloudBlobContainer blobcontainer = blobclient.GetContainerReference("uploadfiles-123");
        await blobcontainer.CreateIfNotExistsAsync();
        // 获取文件路径
        string sourcePath = @"C:\home\bigfiles0120.zip";
        CloudBlockBlob docBlob = blobcontainer.GetBlockBlobReference("bigfiles-2");
        await docBlob.DeleteIfExistsAsync();
        // 设置并发操作的数量
        TransferManager.Configurations.ParallelOperations = 64;
        // 设置单块 blob 的大小,它必须在 4MB 到 100MB 之间,并且是 4MB 的倍数,默认情况下是 4MB
        TransferManager.Configurations.BlockSize = 64 * 1024 * 1024;
        // 设置传输上下文并跟踪上传进度
        var context = new SingleTransferContext();
        UploadOptions uploadOptions = new UploadOptions
        {
            DestinationAccessCondition = AccessCondition.GenerateIfExistsCondition()
        };
        context.ProgressHandler = new Progress<TransferStatus>(progress =>
        {
            //显示上传进度
            Console.WriteLine("Bytes uploaded: {0}", progress.BytesTransferred);
        });
        // 使用 Stopwatch 查看上传所需时间
        var timer = System.Diagnostics.Stopwatch.StartNew();
        // 上传 Blob
        TransferManager.UploadAsync(sourcePath, docBlob, uploadOptions, context, CancellationToken.None).Wait();
        timer.Stop();
        Console.WriteLine("Time (millisecond):" + timer.ElapsedMilliseconds);
        Console.WriteLine("upload success");
    }
}

 

参考资料

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

 

相关文章
|
20天前
|
API
【Azure 媒体服务】Media Service的编码示例 -- 创建缩略图子画面的.NET代码调试问题
【Azure 媒体服务】Media Service的编码示例 -- 创建缩略图子画面的.NET代码调试问题
|
20天前
|
Linux C++ Windows
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
|
20天前
|
API
【Azure Key Vault】.NET 代码如何访问中国区的Key Vault中的机密信息(Get/Set Secret)
【Azure Key Vault】.NET 代码如何访问中国区的Key Vault中的机密信息(Get/Set Secret)
|
20天前
|
存储 API 开发工具
【Azure Storage Blob】如何通过.NET Azure Storage Blobs SDK获取到Blob的MD5值呢?
【Azure Storage Blob】如何通过.NET Azure Storage Blobs SDK获取到Blob的MD5值呢?
|
20天前
|
存储 NoSQL Redis
【Azure Developer】一个复制Redis Key到另一个Redis服务的工具(redis_copy_net8)
【Azure Developer】一个复制Redis Key到另一个Redis服务的工具(redis_copy_net8)
【Azure Developer】一个复制Redis Key到另一个Redis服务的工具(redis_copy_net8)
|
20天前
|
XML API 图形学
【Azure Developer】.Net 简单示例 "文字动图显示" Typing to SVG
【Azure Developer】.Net 简单示例 "文字动图显示" Typing to SVG
【Azure Developer】.Net 简单示例 "文字动图显示" Typing to SVG
|
17天前
|
监控 Cloud Native 开发者
云端精英的.NET微服务秘籍:Azure上的创新实战演练
【8月更文挑战第28天】在现代软件开发中,微服务架构通过分解应用程序提升可维护性和扩展性。结合Azure与.NET框架,开发者能轻松打造高效且易管理的云原生微服务。首先,使用Docker容器化.NET应用,并借助Azure Kubernetes Service(AKS)或Azure Container Instances(ACI)部署。为确保高可用性和伸缩性,可利用Azure Traffic Manager负载均衡及Azure Autoscale动态调整实例数。
22 0
|
20天前
|
开发框架 .NET C#
【Azure Developer】C# / .NET 静态函数中this关键字的作用
【Azure Developer】C# / .NET 静态函数中this关键字的作用
|
20天前
|
存储 Linux 网络安全
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
|
20天前
|
开发框架 前端开发 JavaScript
【Azure App Service】.NET应用读取静态文件时遇见了404错误的解决方法
【Azure App Service】.NET应用读取静态文件时遇见了404错误的解决方法