105067 MinIO调试

This commit is contained in:
SINVO\yangshunli 2024-11-04 15:11:14 +08:00
parent 873d7bbd01
commit 52be00103c
7 changed files with 412 additions and 21 deletions

View File

@ -323,6 +323,33 @@
<param name="code"></param>
<returns></returns>
</member>
<member name="M:LFlow.Base.Utils.BucketManager.IsExistStr(System.String)">
<summary>
1、判断bucket是否存在
</summary>
<param name="bucketName"></param>
<returns></returns>
</member>
<member name="M:LFlow.Base.Utils.BucketManager.Create(System.String)">
<summary>
2、创建一个bucket
</summary>
<param name="bucketName"></param>
<returns></returns>
</member>
<member name="M:LFlow.Base.Utils.BucketManager.Delete(System.String)">
<summary>
3、移除一个bucket
</summary>
<param name="bucketName"></param>
<returns></returns>
</member>
<member name="M:LFlow.Base.Utils.BucketManager.GetList">
<summary>
4、获取已有的bucket列表
</summary>
<returns></returns>
</member>
<member name="T:LFlow.Base.Utils.CodeFirst">
<summary>
CodeFirst
@ -370,6 +397,18 @@
<param name="model"></param>
<returns></returns>
</member>
<member name="M:LFlow.Base.Utils.MinIOService.GetBuckets">
<summary>
获取桶列表
</summary>
<returns></returns>
</member>
<member name="M:LFlow.Base.Utils.MinIOService.DownLoad">
<summary>
下载接口
</summary>
<returns></returns>
</member>
<member name="T:LFlow.Base.Utils.ObjectToWhereExp">
<summary>
对象转换为where条件

View File

@ -15,6 +15,7 @@
<ItemGroup>
<PackageReference Include="Mapster" Version="7.4.1-pre01" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0-rc.2.24473.5" />
<PackageReference Include="Minio" Version="6.0.3" />
<PackageReference Include="Serilog" Version="4.1.1-dev-02318" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />

View File

@ -2,13 +2,17 @@
using LFlow.Base.Utils;
using LFlow.Cache;
using LFlow.InternalEventBus;
using Minio;
using LFlow.Middleware;
using LFlow.Middleware.Register;
using Serilog;
using Serilog.Events;
using SqlSugar;
using System.Reflection;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
namespace LFlow.Base;
/// <summary>
@ -63,6 +67,19 @@ public static class Program
{
c.Conventions.Add(new ApiExplorerGroupPerVersionConvention());
});
string endPoint = "172.30.20.40:9000";
string accessKey = "name";
string secretKey = "password";
bool secure = true;
builder.Services.AddSingleton<IMinioClient>
(s =>
new MinioClient()
.WithEndpoint(endPoint)
.WithCredentials(accessKey, secretKey)
.WithSSL(secure)
.Build()
);
builder.Services.AddSwaggerGen(u =>
{
@ -180,6 +197,7 @@ public static class Program
app.Run();
}
/// <summary>
/// 配置SqlSugar
/// </summary>

View File

@ -0,0 +1,138 @@
using Minio.DataModel.Args;
using Minio.DataModel.Result;
using Minio.Exceptions;
using Minio;
using Minio.DataModel.Args;
using Minio.Exceptions;
using Minio;
using Minio.DataModel.Result;
namespace LFlow.Base.Utils
{
public class BucketManager
{
public static IMinioClient minioClient;
public BucketManager(IMinioClient IminioClient) {
minioClient= IminioClient;
}
/// <summary>
/// 1、判断bucket是否存在
/// </summary>
/// <param name="bucketName"></param>
/// <returns></returns>
public static async Task<string> IsExistStr(string bucketName)
{
try
{
BucketExistsArgs args = new BucketExistsArgs().WithBucket(bucketName);
bool found = await minioClient.BucketExistsAsync(args).ConfigureAwait(false);
Console.WriteLine("found。。。。。。", found);
if (found)
{
Console.WriteLine($"{bucketName}桶已存在");
return $"{bucketName}桶已存在";
}
else
{
Console.WriteLine($"{bucketName}桶不存在");
return $"{bucketName}桶不存在";
}
}
catch (MinioException e)
{
Console.WriteLine("[Bucket] Exception: {0}", e);
return "出错啦!!!";
}
}
/// <summary>
/// 2、创建一个bucket
/// </summary>
/// <param name="bucketName"></param>
/// <returns></returns>
public static async Task<string> Create(string? bucketName)
{
try
{
BucketExistsArgs args = new BucketExistsArgs().WithBucket(bucketName);
bool found = await minioClient.BucketExistsAsync(args).ConfigureAwait(false);
if (found)
{
return $"{bucketName}桶已存在";
}
else
{
MakeBucketArgs makeBucketArgs = new MakeBucketArgs().WithBucket(bucketName);
await minioClient.MakeBucketAsync(makeBucketArgs).ConfigureAwait(false);
return $"{bucketName}桶已成功创建";
}
}
catch (MinioException e)
{
Console.WriteLine("[Bucket] Exception: {0}", e);
return "出错啦!!!";
}
}
/// <summary>
/// 3、移除一个bucket
/// </summary>
/// <param name="bucketName"></param>
/// <returns></returns>
public static async Task<string> Delete(string? bucketName)
{
try
{
BucketExistsArgs args = new BucketExistsArgs().WithBucket(bucketName);
bool found = await minioClient.BucketExistsAsync(args).ConfigureAwait(false);
if (!found)
{
return $"{bucketName}桶不存在";
}
else
{
RemoveBucketArgs removeBucketArgs = new RemoveBucketArgs().WithBucket(bucketName);
await minioClient.RemoveBucketAsync(removeBucketArgs);
return $"{bucketName}桶删除成功";
}
}
catch (MinioException e)
{
Console.WriteLine("[Bucket] Exception: {0}", e);
return "出错啦!!!";
}
}
/// <summary>
/// 4、获取已有的bucket列表
/// </summary>
/// <returns></returns>
public static async Task<ListAllMyBucketsResult?> GetList()
{
try
{
return await minioClient.ListBucketsAsync();
}
catch (MinioException e)
{
Console.WriteLine("Error occurred: " + e);
return null;
}
}
}
}

View File

@ -0,0 +1,168 @@
using Microsoft.VisualBasic.FileIO;
using Minio;
using Minio.DataModel;
using Minio.DataModel.Args;
using Minio.Exceptions;
using System;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace LFlow.Base.Utils
{
public class MinIOService
{
private readonly IMinioClient minioClient;
private readonly BucketManager bucketManager;
private static string bucketName = "kecheng";
public MinIOService(IMinioClient _minioClient)
{
minioClient = _minioClient;
bucketManager = new BucketManager(_minioClient);
}
public async Task<string> Upload(IFormFile file)
{
try
{
//logger.LogInformation("文件开始上传");
string uploadsPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "uploads");
if (!Directory.Exists(uploadsPath))
{
Directory.CreateDirectory(uploadsPath);
}
string fileName = file.FileName;
string filePath = Path.Combine(uploadsPath, fileName);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
string fileExtension = Path.GetExtension(fileName);
string newFileName = null;
int counter = 1;
while (File.Exists(filePath))
{
//重新命名文件
string Name = $"{fileNameWithoutExtension}_{counter}{fileExtension}";
filePath = Path.Combine(uploadsPath, Name);
newFileName = Path.GetFileNameWithoutExtension(filePath);
counter++;
}
if (newFileName == null)
{
newFileName = Path.GetFileNameWithoutExtension(fileName);
}
using (var stream = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(stream);
}
//AddFileInfo(filePath, Path.GetFileNameWithoutExtension(fileName), newFileName);
var beArgs = new BucketExistsArgs()
.WithBucket(bucketName);
bool found = await minioClient.BucketExistsAsync(beArgs);
if (!found)
{
var mbArgs = new MakeBucketArgs()
.WithBucket(bucketName);
await minioClient.MakeBucketAsync(mbArgs).ConfigureAwait(false);
}
// 设置上传文件的对象名
var objectName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
var putObjectArgs = new PutObjectArgs()
.WithBucket(bucketName)
.WithObject(objectName)
.WithFileName(filePath)
.WithContentType(file.ContentType);
await minioClient.PutObjectAsync(putObjectArgs).ConfigureAwait(false);
Console.WriteLine($"文件 '{filePath}' 上传到 bucket '{bucketName}' 中,文件名为 '{objectName}'。");
return objectName;
}
catch (Exception ex)
{
// 处理异常
throw new ApplicationException("文件上传失败", ex);
}
}
/// <summary>
/// 获取桶列表
/// </summary>
/// <returns></returns>
public async Task<List<string>> GetBuckets()
{
var list = await minioClient.ListBucketsAsync().ConfigureAwait(false);
var buckets = list.Buckets.Select(b => b.Name).ToList();
return buckets;
}
/// <summary>
/// 下载接口
/// </summary>
/// <returns></returns>
public async Task DownLoad()
{
try
{
StatObjectArgs statObjectArgs = new StatObjectArgs()
.WithBucket(bucketName)
.WithObject("替换你上传到minio服务器中生成的objectName");
await minioClient.StatObjectAsync(statObjectArgs);
GetObjectArgs getObjectArgs = new GetObjectArgs()
.WithBucket(bucketName)
.WithObject("替换你上传到minio服务器中生成的objectName")
.WithFile("D:\\photo.jpg"); //替换你实际的文件输出路径
await minioClient.GetObjectAsync(getObjectArgs);
}
catch (MinioException e)
{
Console.Out.WriteLine("Error occurred:" + e);
}
}
/// <summary>
/// 后缀名枚举
/// </summary>
/// <param name="fileExtension"></param>
/// <returns></returns>
//public string DetermineFileType(string fileExtension)
//{
// HashSet<string> supportedImageExtensions = new HashSet<string>
// {
// ".jpg",".jpeg", ".png",".gif",".bmp",".tiff",".tif",".svg",".webp", ".ico"
// };
// HashSet<string> supportedVideoExtensions = new HashSet<string>
// {
// ".mp4",".avi",".mkv",".mov",".wmv",".flv",".webm"
// };
// HashSet<string> supportedDocumentExtensions = new HashSet<string>
// {
// ".doc",".docx",".pdf",".txt",".ppt",".pptx",".xls",".xlsx",
// };
// if (supportedDocumentExtensions.Contains(fileExtension.ToLower()))
// {
// return FileType.document.ToString();
// }
// else if (supportedImageExtensions.Contains(fileExtension.ToLower()))
// {
// return FileType.image.ToString();
// }
// else if (supportedVideoExtensions.Contains(fileExtension.ToLower()))
// {
// return FileType.video.ToString();
// }
// else
// {
// return FileType.other.ToString();
// }
}
}

View File

@ -1,22 +1,28 @@
{
"Urls": "https://127.0.0.1:8443;http://127.0.0.1:8088",
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft.AspNetCore.Mvc": "Debug",
"Microsoft.AspNetCore.Routing": "Debug",
"Microsoft.AspNetCore.Hosting": "Debug"
}
"Urls": "https://172.30.20.40:3452;http://172.30.20.40:3453",
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft.AspNetCore.Mvc": "Debug",
"Microsoft.AspNetCore.Routing": "Debug",
"Microsoft.AspNetCore.Hosting": "Debug"
}
},
"WriteTo": [
{
"Name": "Console"
}
]
},
"WriteTo": [
{
"Name": "Console"
}
]
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Data Source=192.168.3.85;Initial Catalog=PluginAdmin-dev;User Id=sa;Password=Sa1234;Encrypt=True;TrustServerCertificate=True"
}
"MinIoConfig": {
"EndPoint": "localhost:9000",
"AccessKey": "name",
"SecretKey": "passsword",
"UseSSL": false
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Data Source=192.168.3.85;Initial Catalog=PluginAdmin-dev;User Id=sa;Password=Sa1234;Encrypt=True;TrustServerCertificate=True"
}
}

View File

@ -4,11 +4,17 @@ using LFlow.VersionManagement.Enums;
using LFlow.VersionManagement.Model;
using LFlow.VersionManagement.Service;
using Mapster;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Minio;
using SqlSugar;
namespace LFlow.VersionManagement.Controller;
public class VersionManagementController(IVersionManagementService service) : BaseController
public class VersionManagementController(IVersionManagementService service, IMinioClient _client) : BaseController
{
[HttpDelete]
public int DeleteById(string id)
{
@ -55,6 +61,21 @@ public class VersionManagementController(IVersionManagementService service) : Ba
var result = service.Save(entity, isUpdate).Adapt<VersionDto>();
return ApiResult<VersionDto>.SuccessResult(result);
}
/// <summary>
/// 保存文件
/// </summary>
/// <param name="entity"></param>
/// <param name="isUpdate"></param>
/// <returns></returns>
[HttpPost]
public async Task<IResult> SaveFile(IFormFile formFile, bool isUpdate)
{
MinIOService minIOService = new MinIOService(_client);
var guid= minIOService.Upload(formFile);
return (IResult)Ok(guid.Result);
}
[HttpPost]
public List<VersionDto> Search(VersionDto whereObj)