Simple WPF: S3实现MINIO大文件上传并显示上传进度

最新内容优先发布于个人博客:小虎技术分享站,随后逐步搬运到博客园。

创作不易,如果觉得有用请在Github上为博主点亮一颗小星星吧!

目的

早两天写了一篇S3简单上传文件的小工具,知乎上看到了一个问题问如何实现显示MINIO上传进度,因此拓展一下这个小工具能够在上传大文件时显示进度。

完整代码托管于Github:mrchipset/simple-wpf

实现方式

  1. 先通过Xaml编写一个包含上传进度条的小界面。具体内容就不赘述了,可以参考这篇文章
  2. 为了得到上传进度就不能再简单地使用PutObjectRequest 进行上传需要使用S3中TransferUtility 提供的高等级API进行上传。
  3. 然后创建一个TransferUtilityUploadRequest 对象并绑定其UploadProgressEvent 事件以实现上传进度的监控

具体的实现代码如下:

c# 复制代码
private async Task<bool> UploadLargeFileAsync()
{
    var credentials = new BasicAWSCredentials(_accessKey, _secretKey);

    var clientConfig = new AmazonS3Config
    {
        ForcePathStyle = true,
        ServiceURL = _endpoint,
    };

    bool ret = true;
    using (var client = new AmazonS3Client(credentials, clientConfig))
    {


        try
        {
            var fileTransferUtility = new TransferUtility(client);

            var uploadRequest = new TransferUtilityUploadRequest
            {
                BucketName = LargeBucket,
                FilePath = UploadLargeFile,
                Key = System.IO.Path.GetFileName(UploadLargeFile)
            };

            uploadRequest.UploadProgressEvent += UploadRequest_UploadProgressEvent;

            await fileTransferUtility.UploadAsync(uploadRequest);
        }
        catch (FileNotFoundException e)
        {
            ret = false;
            this.Dispatcher.Invoke(new Action(() => this.statusLargeTxtBlk.Text = e.Message));
        }
        catch (AmazonS3Exception e)
        {
            ret = false;
            if (e.ErrorCode != null &&
                (e.ErrorCode.Equals("InvalidAccessKeyId") ||
            e.ErrorCode.Equals("InvalidSecurity")))
            {
                this.Dispatcher.Invoke(new Action(() => this.statusLargeTxtBlk.Text = "Please check the provided AWS Credentials"));
            }
            else
            {
                this.Dispatcher.Invoke(new Action(() => this.statusLargeTxtBlk.Text = $"An error occurred with the message '{e.Message}' when writing an object"));
            }
        }
        catch(Exception e)
        {
            this.Dispatcher.Invoke(new Action(() => this.statusLargeTxtBlk.Text = $"An error occurred with the message '{e.Message}' when writing an object"));
        }
    }
    return ret;
}

private void UploadRequest_UploadProgressEvent(object? sender, UploadProgressArgs e)
{
    this.Dispatcher.Invoke((Action)(() =>
    {
        this.uploadProgress.Value = e.TransferredBytes * 100 / e.TotalBytes ;
    }));
}

值得一提的时,在上传进度的事件处理函数中,由于我们通过异步方法执行上传函数,因此我们需要使用Dispatcher 来更新数据到UI 上。

演示效果

参考连接

https://docs.aws.amazon.com/AmazonS3/latest/userguide/mpu-upload-object.html

https://www.xtigerkin.com/archives/96/

相关推荐
CodeCraft Studio9 小时前
3D文档控件Aspose.3D实用教程:使用 C# 构建 OBJ 到 U3D 转换器
开发语言·3d·c#·3d渲染·aspose·3d文件格式转换·3d sdk
唐青枫10 小时前
ValueTask 实战指南:解锁 .NET 异步编程的性能秘密
c#·.net
chenglin01610 小时前
C#_gRPC
开发语言·c#
★YUI★14 小时前
学习制作记录(选项UI以及存档系统)8.24
学习·游戏·ui·unity·c#
liulilittle15 小时前
.NET反射与IL反编译核心技术
开发语言·数据库·c#·.net·反射·反编译·il
月巴月巴白勺合鸟月半15 小时前
Teams Bot机器人实时语音识别的多引擎的处理
机器人·c#
hixiong12317 小时前
C# 编写一个XmlToDota的转换工具
开发语言·人工智能·yolo·c#
用户37215742613518 小时前
C# 高保真 Word 转 HTML 实战指南
c#
gloomyfish18 小时前
【零代码】OpenCV C# 快速开发框架演示
人工智能·opencv·c#
诸葛务农21 小时前
人形机器人——电子皮肤技术路线:光学式电子皮肤及MIT基于光导纤维的分布式触觉传感电子皮肤
分布式·机器人·wpf