From b39f5569a73d755590e5f62f3d9c5adacdd6ff8e Mon Sep 17 00:00:00 2001
From: Bunny <1319900154@qq.com>
Date: Mon, 19 Aug 2024 22:43:35 +0800
Subject: [PATCH] =?UTF-8?q?feat(=E6=96=B0=E5=A2=9E):=20Minio=E4=B8=8A?=
=?UTF-8?q?=E4=BC=A0=E6=96=87=E4=BB=B6=E5=92=8C=E5=88=97=E5=87=BA=E6=89=80?=
=?UTF-8?q?=E4=BB=A5=E6=A1=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Bunny.Service/IService/IMinioService.cs | 19 +++++++
.../IService/Service/MinioService.cs | 54 +++++++++++++++++++
Bunny.WebApi/Config/BaseConfig.cs | 1 +
Bunny.WebApi/Config/ServiceRegistration.cs | 4 +-
Bunny.WebApi/Controllers/MinioController.cs | 33 ++++++++++++
Bunny.WebApi/appsettings.json | 2 +-
6 files changed, 110 insertions(+), 3 deletions(-)
create mode 100644 Bunny.Service/IService/IMinioService.cs
create mode 100644 Bunny.Service/IService/Service/MinioService.cs
create mode 100644 Bunny.WebApi/Controllers/MinioController.cs
diff --git a/Bunny.Service/IService/IMinioService.cs b/Bunny.Service/IService/IMinioService.cs
new file mode 100644
index 0000000..7bd2e72
--- /dev/null
+++ b/Bunny.Service/IService/IMinioService.cs
@@ -0,0 +1,19 @@
+using Microsoft.AspNetCore.Http;
+using Minio.DataModel.Result;
+
+namespace Bunny.Service.IService;
+
+public interface IMinioService
+{
+ ///
+ /// 上传文件
+ ///
+ /// 文件内容
+ void SaveMinioFile(IFormFile? file);
+
+ ///
+ /// 查询所有的桶
+ ///
+ /// 桶列表
+ ListAllMyBucketsResult GetAllMyBuckets();
+}
\ No newline at end of file
diff --git a/Bunny.Service/IService/Service/MinioService.cs b/Bunny.Service/IService/Service/MinioService.cs
new file mode 100644
index 0000000..455350c
--- /dev/null
+++ b/Bunny.Service/IService/Service/MinioService.cs
@@ -0,0 +1,54 @@
+using Bunny.Common.Connect;
+using Microsoft.AspNetCore.Http;
+using Minio;
+using Minio.DataModel.Args;
+using Minio.DataModel.Result;
+
+namespace Bunny.Service.IService.Service;
+
+public class MinioService : IMinioService
+{
+ private readonly IMinioClient _minioContext = MinioContext.MinioClient!;
+
+ ///
+ /// 上传文件
+ ///
+ /// 文件内容
+ public async void SaveMinioFile(IFormFile? file)
+ {
+ if (file == null)
+ {
+ Console.WriteLine("文件为空");
+ return;
+ }
+
+ var fileName = file.FileName;
+ var contentType = file.ContentType;
+ var putObjectResponse = await _minioContext.PutObjectAsync(new PutObjectArgs()
+ .WithBucket(MinioContext.BucketName)
+ .WithObject(fileName)
+ .WithStreamData(file.OpenReadStream())
+ .WithContentType(contentType)
+ .WithObjectSize(file.Length)
+ );
+
+ Console.WriteLine($"putObjectResponse.ObjectNam---{putObjectResponse.ObjectName}");
+ }
+
+ ///
+ /// 查询所有的桶
+ ///
+ /// 桶列表
+ public ListAllMyBucketsResult GetAllMyBuckets()
+ {
+ var listBucketsAsync = _minioContext!.ListBucketsAsync().Result;
+ foreach (var bucket in listBucketsAsync.Buckets)
+ {
+ Console.WriteLine(bucket.Name);
+ Console.WriteLine(bucket.CreationDate);
+ Console.WriteLine(bucket.CreationDateDateTime);
+ }
+
+ return listBucketsAsync;
+ }
+}
\ No newline at end of file
diff --git a/Bunny.WebApi/Config/BaseConfig.cs b/Bunny.WebApi/Config/BaseConfig.cs
index 5bf3015..0dca32e 100644
--- a/Bunny.WebApi/Config/BaseConfig.cs
+++ b/Bunny.WebApi/Config/BaseConfig.cs
@@ -19,6 +19,7 @@ public class BaseConfig(WebApplicationBuilder builder)
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
// 添加 SignalR
builder.Services.AddSignalR();
+ builder.WebHost.ConfigureKestrel((context, options) => { options.Limits.MaxRequestBodySize = 1073741824; });
// 添加Service服务
builder.AddApplicationServices();
// 添加后台服务
diff --git a/Bunny.WebApi/Config/ServiceRegistration.cs b/Bunny.WebApi/Config/ServiceRegistration.cs
index 9398b0c..876c2b8 100644
--- a/Bunny.WebApi/Config/ServiceRegistration.cs
+++ b/Bunny.WebApi/Config/ServiceRegistration.cs
@@ -3,7 +3,6 @@ using Bunny.Common.Filter;
using Bunny.Service.Filter;
using Bunny.Service.IService;
using Bunny.Service.IService.Service;
-using Bunny.Service.Job.JobService;
using Lazy.Captcha.Core;
using Lazy.Captcha.Core.Generator;
using SkiaSharp;
@@ -22,6 +21,7 @@ public static class ServiceRegistration
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
+ builder.Services.AddScoped();
}
///
@@ -35,7 +35,7 @@ public static class ServiceRegistration
// builder.Services.AddHostedService().Clear();
// https://github.com/quartznet/quartznet/tree/main/database/tables
// builder.AddCreateSimpleJobService();
- builder.AddCronJobService();
+ // builder.AddCronJobService();
}
///
diff --git a/Bunny.WebApi/Controllers/MinioController.cs b/Bunny.WebApi/Controllers/MinioController.cs
new file mode 100644
index 0000000..2e7854a
--- /dev/null
+++ b/Bunny.WebApi/Controllers/MinioController.cs
@@ -0,0 +1,33 @@
+using Bunny.Dao.Result;
+using Bunny.Service.IService;
+using Microsoft.AspNetCore.Mvc;
+using Minio.DataModel.Result;
+
+namespace Bunny.WebApi.Controllers;
+
+[Route("/api/[controller]/[action]")]
+[ApiController]
+public class MinioController(IMinioService minioService) : ControllerBase
+{
+ ///
+ /// 查询所有的桶
+ ///
+ ///
+ [HttpGet]
+ public Result GetAllMyBuckets()
+ {
+ var listAllMyBucketsResult = minioService.GetAllMyBuckets();
+ return Result.Success(listAllMyBucketsResult);
+ }
+
+ ///
+ /// 上传文件
+ ///
+ /// 文件
+ [HttpPost]
+ public Result SaveMinioFile(IFormFile? file)
+ {
+ minioService.SaveMinioFile(file);
+ return Result.Success("上传成功");
+ }
+}
\ No newline at end of file
diff --git a/Bunny.WebApi/appsettings.json b/Bunny.WebApi/appsettings.json
index e3ef6b2..28b0840 100644
--- a/Bunny.WebApi/appsettings.json
+++ b/Bunny.WebApi/appsettings.json
@@ -26,6 +26,6 @@
"Url": "192.168.3.98:9000",
"AccessKey": "bunny",
"SecretKey": "02120212",
- "BucketName": "test"
+ "BucketName": "csharp-test"
}
}
\ No newline at end of file