Simple WPF: S3实现MINIO大文件上传并显示上传进度
Simple WPF: S3实现MINIO大文件上传并显示上传进度
1.Simple WPF:WPF 透明窗体和鼠标事件穿透07-012.Simple WPF:WPF 自定义按钮外形07-073.Simple WPF:WPF 实现按钮的长按,短按功能07-084.Simple WPF:WPF自定义一个可以定义步长的SpinBox07-095.Simple WPF:C# 使用基本的async/await实现异步07-096.Simple WPF:C# Task异步任务的取消初探07-097.Simple WPF:WPF实现一个MINIO等S3兼容对象存储上传文件的小工具07-10
8.Simple WPF:S3实现MINIO大文件上传并显示上传进度07-11
9.Simple WPF:WPF使用Windows API发送Toast通知07-15最新内容优先发布于个人博客:小虎技术分享站,随后逐步搬运到博客园。
创作不易,如果觉得有用请在Github上为博主点亮一颗小星星吧!
目的#
早两天写了一篇S3简单上传文件的小工具,知乎上看到了一个问题问如何实现显示MINIO上传进度,因此拓展一下这个小工具能够在上传大文件时显示进度。
完整代码托管于Github:mrchipset/simple-wpf
实现方式#
- 先通过
Xaml
编写一个包含上传进度条的小界面。具体内容就不赘述了,可以参考这篇文章 - 为了得到上传进度就不能再简单地使用
PutObjectRequest
进行上传需要使用S3中TransferUtility
提供的高等级API进行上传。 - 然后创建一个
TransferUtilityUploadRequest
对象并绑定其UploadProgressEvent
事件以实现上传进度的监控
具体的实现代码如下:
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
漫思
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
2022-07-19 拍下宇宙的韦伯空间望远镜,其飞行软件采用 C++ 编写、内置定制的 JS 解释器
2022-07-19 Hype 4.0(Mac系统)的布局功能
2022-07-19 Immer 入门
2022-07-19 设计克制
2022-07-19 adobe工具软件应用
2022-07-19 高性能动画的
2022-07-19 RN Weex的超过100片