54 lines
1.8 KiB
C#
54 lines
1.8 KiB
C#
using System.IO;
|
|
using Minio.DataModel;
|
|
using Minio.DataModel.Args;
|
|
|
|
namespace Hopetry.Services
|
|
{
|
|
public partial class MinioService
|
|
{
|
|
#region 多文件上传
|
|
public async void UploadFileToMinIOWithProgress(string filePath)
|
|
{
|
|
try
|
|
{
|
|
string bucketName = "test";
|
|
string objectName = Path.GetFileName(filePath);
|
|
|
|
//判断桶是否存在
|
|
var beArgs = new BucketExistsArgs().WithBucket(bucketName);
|
|
bool found = await _minioClient.BucketExistsAsync(beArgs).ConfigureAwait(false);
|
|
if (!found)
|
|
{
|
|
var mbArgs = new MakeBucketArgs()
|
|
.WithBucket(bucketName);
|
|
await _minioClient.MakeBucketAsync(mbArgs).ConfigureAwait(false);
|
|
}
|
|
|
|
var progress = new Progress<ProgressReport>(progressReport =>
|
|
{
|
|
//Dispatcher.Invoke(() =>
|
|
//{
|
|
// progressBar.Value = progressReport.Percentage;// 更新进度条的值
|
|
//});
|
|
});
|
|
|
|
PutObjectArgs putObjectArgs = new PutObjectArgs()
|
|
.WithBucket(bucketName)
|
|
.WithObject(objectName)
|
|
.WithFileName(filePath)
|
|
//.WithContentType("application/octet-stream")
|
|
.WithProgress(progress);
|
|
|
|
// 上传文件并提供进度反馈
|
|
await _minioClient.PutObjectAsync(putObjectArgs);
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception($"上传文件失败: {ex.Message}");
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|