How to get the progress of uploading files in C# minio

The main method is to use the log recording method in MinioClient to find the progress of the upload from the log. Minio divides the large file into small files of 5MB to upload.

//
// 摘要:
// Sets HTTP tracing On.Writes output to Console
public void SetTraceOn(IRequestLogger logger = null);

//fileSizeKB 总文件字节数
//计算minio分割的总文件块、minio将大文件切割为每5MB一个小文件上传
int total_minio_file_count = (int)Math.Ceiling(fileSizeKB / 5120);

var client = new MinioClient(
                                        endpoint: "192.168.1.117:9000",
                                        accessKey: "minio",
                                        secretKey: "minio123456"
                  );
client.SetTraceOn(new MinioRequestLog()
{
    
    
	//日志记录
	BackLogFunc = fileNo =>
	{
    
    
		decimal process = Math.Round((decimal)fileNo * 100 / total_minio_file_count, 2);
		string videoProcess = $"后台上传进度:{process}%";
		LogHelpter.AddLog(videoProcess);
		
		//signalR推送到前端显示进度值
		hubContext.Clients.User(loginUserId).SendAsync("ReceiveMessage", loginUserId, process);
	}
});

await client.PutObjectAsync(bucketName, fileDbNew, fileSavePath);
stopwatch.Stop();
LogHelpter.AddLog($"上传成功,耗时{stopwatch.ElapsedMilliseconds}毫秒,文件" + fileDbNew + ",文件id=" + fileId);

//上传成功后,删除本地文件
System.IO.File.Delete(fileSavePath);

Logging

using Minio;
using Minio.DataModel.Tracing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;


namespace WebNetCore5_Img_Storage.Model.Tool
{
    
    
    /// <summary>
    /// mino请求日志记录
    /// </summary>
    /// <remarks>
    /// </remarks>
    public class MinioRequestLog : IRequestLogger
    {
    
    
        /// <summary>
        /// 进度值回调处理
        /// </summary>
        public Action<int> BackLogFunc {
    
     get; set; }

        /// <summary>
        /// 日志回写方法
        /// </summary>
        /// <param name="requestToLog"></param>
        /// <param name="responseToLog"></param>
        /// <param name="durationMs"></param>
        public void LogRequest(RequestToLog requestToLog, ResponseToLog responseToLog, double durationMs)
        {
    
    
            if (responseToLog.statusCode == HttpStatusCode.OK)
            {
    
    
                foreach (var header in requestToLog.parameters)
                {
    
    
                    if (header.value == null) continue;
                    //LogHelpter.AddLog($"minio上传{header.name}:{header.value.ToString()}");
                    //partNumber 文件块序号,从1开始
                    if (header.name.Equals("partNumber"))
                    {
    
    
                        int partNumber = Convert.ToInt32(header.value);
                        BackLogFunc(partNumber);
                    }
                }

            }
        }
    }
}

Guess you like

Origin blog.csdn.net/u011511086/article/details/114398348