feat(新增): Minio上传文件和列出所以桶
This commit is contained in:
parent
4f429d09a4
commit
b39f5569a7
|
@ -0,0 +1,19 @@
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using Minio.DataModel.Result;
|
||||
|
||||
namespace Bunny.Service.IService;
|
||||
|
||||
public interface IMinioService
|
||||
{
|
||||
/// <summary>
|
||||
/// 上传文件
|
||||
/// </summary>
|
||||
/// <param name="file">文件内容</param>
|
||||
void SaveMinioFile(IFormFile? file);
|
||||
|
||||
/// <summary>
|
||||
/// 查询所有的桶
|
||||
/// </summary>
|
||||
/// <returns>桶列表</returns>
|
||||
ListAllMyBucketsResult GetAllMyBuckets();
|
||||
}
|
|
@ -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!;
|
||||
|
||||
/// <summary>
|
||||
/// 上传文件
|
||||
/// </summary>
|
||||
/// <param name="file">文件内容</param>
|
||||
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}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询所有的桶
|
||||
/// </summary>
|
||||
/// <returns>桶列表</returns>
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
// 添加后台服务
|
||||
|
|
|
@ -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<IBlogService, BlogService>();
|
||||
builder.Services.AddScoped<IRedisOptionService, RedisOptionService>();
|
||||
builder.Services.AddScoped<IJobService, JobService>();
|
||||
builder.Services.AddScoped<IMinioService, MinioService>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -35,7 +35,7 @@ public static class ServiceRegistration
|
|||
// builder.Services.AddHostedService<TemplateBackgroundModule>().Clear();
|
||||
// https://github.com/quartznet/quartznet/tree/main/database/tables
|
||||
// builder.AddCreateSimpleJobService();
|
||||
builder.AddCronJobService();
|
||||
// builder.AddCronJobService();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 查询所有的桶
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public Result<ListAllMyBucketsResult> GetAllMyBuckets()
|
||||
{
|
||||
var listAllMyBucketsResult = minioService.GetAllMyBuckets();
|
||||
return Result<ListAllMyBucketsResult>.Success(listAllMyBucketsResult);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 上传文件
|
||||
/// </summary>
|
||||
/// <param name="file">文件</param>
|
||||
[HttpPost]
|
||||
public Result<string> SaveMinioFile(IFormFile? file)
|
||||
{
|
||||
minioService.SaveMinioFile(file);
|
||||
return Result<string>.Success("上传成功");
|
||||
}
|
||||
}
|
|
@ -26,6 +26,6 @@
|
|||
"Url": "192.168.3.98:9000",
|
||||
"AccessKey": "bunny",
|
||||
"SecretKey": "02120212",
|
||||
"BucketName": "test"
|
||||
"BucketName": "csharp-test"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue