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