经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
Simple WPF: S3实现MINIO大文件上传并显示上传进度
来源:cnblogs  作者:mrchip  时间:2024/7/11 21:17:29  对本文有异议

最新内容优先发布于个人博客:小虎技术分享站,随后逐步搬运到博客园。
创作不易,如果觉得有用请在Github上为博主点亮一颗小星星吧!

目的

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

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

80XgKMHuN9beh6D86Zzup9KEG94t3qdNlx5aN_AuW-U.png

实现方式

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

具体的实现代码如下:

  1. private async Task<bool> UploadLargeFileAsync()
  2. {
  3. var credentials = new BasicAWSCredentials(_accessKey, _secretKey);
  4. var clientConfig = new AmazonS3Config
  5. {
  6. ForcePathStyle = true,
  7. ServiceURL = _endpoint,
  8. };
  9. bool ret = true;
  10. using (var client = new AmazonS3Client(credentials, clientConfig))
  11. {
  12. try
  13. {
  14. var fileTransferUtility = new TransferUtility(client);
  15. var uploadRequest = new TransferUtilityUploadRequest
  16. {
  17. BucketName = LargeBucket,
  18. FilePath = UploadLargeFile,
  19. Key = System.IO.Path.GetFileName(UploadLargeFile)
  20. };
  21. uploadRequest.UploadProgressEvent += UploadRequest_UploadProgressEvent;
  22. await fileTransferUtility.UploadAsync(uploadRequest);
  23. }
  24. catch (FileNotFoundException e)
  25. {
  26. ret = false;
  27. this.Dispatcher.Invoke(new Action(() => this.statusLargeTxtBlk.Text = e.Message));
  28. }
  29. catch (AmazonS3Exception e)
  30. {
  31. ret = false;
  32. if (e.ErrorCode != null &&
  33. (e.ErrorCode.Equals("InvalidAccessKeyId") ||
  34. e.ErrorCode.Equals("InvalidSecurity")))
  35. {
  36. this.Dispatcher.Invoke(new Action(() => this.statusLargeTxtBlk.Text = "Please check the provided AWS Credentials"));
  37. }
  38. else
  39. {
  40. this.Dispatcher.Invoke(new Action(() => this.statusLargeTxtBlk.Text = $"An error occurred with the message '{e.Message}' when writing an object"));
  41. }
  42. }
  43. catch(Exception e)
  44. {
  45. this.Dispatcher.Invoke(new Action(() => this.statusLargeTxtBlk.Text = $"An error occurred with the message '{e.Message}' when writing an object"));
  46. }
  47. }
  48. return ret;
  49. }
  50. private void UploadRequest_UploadProgressEvent(object? sender, UploadProgressArgs e)
  51. {
  52. this.Dispatcher.Invoke((Action)(() =>
  53. {
  54. this.uploadProgress.Value = e.TransferredBytes * 100 / e.TotalBytes ;
  55. }));
  56. }

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

演示效果

RrcNgGshrDzSeRmjcoMmk44kuWK1DLACH3xXRyIUKNw.gif

参考连接

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

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

原文链接:https://www.cnblogs.com/mrchip/p/18297187

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号