feat(新增): Minio示例完成
This commit is contained in:
parent
b39f5569a7
commit
59012062af
|
@ -1,5 +1,4 @@
|
|||
using Bunny.Common.Utils;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Minio;
|
||||
|
||||
namespace Bunny.Common.Connect;
|
||||
|
@ -10,6 +9,11 @@ public static class MinioContext
|
|||
public static readonly string BucketName = AppSettings.GetConfig("Minio:BucketName");
|
||||
|
||||
public static void AddMinioContext(this WebApplicationBuilder builder)
|
||||
{
|
||||
Task.Run(Connect);
|
||||
}
|
||||
|
||||
private static void Connect()
|
||||
{
|
||||
var endpoint = AppSettings.GetConfig("Minio:Url");
|
||||
var accessKey = AppSettings.GetConfig("Minio:AccessKey");
|
||||
|
@ -18,14 +22,7 @@ public static class MinioContext
|
|||
MinioClient = new MinioClient().WithEndpoint(endpoint).WithCredentials(accessKey, secretKey)
|
||||
// .WithSSL() // 使用HTTPS
|
||||
.Build();
|
||||
Task.Run(ShowAllBucket);
|
||||
}
|
||||
|
||||
private static async Task ShowAllBucket()
|
||||
{
|
||||
var listBucketsAsync = await MinioUtil.ListBucketsAsync();
|
||||
foreach (var bucket in listBucketsAsync.Buckets) Console.WriteLine($"Minio存在的桶:{bucket.Name}");
|
||||
|
||||
Console.WriteLine("Minio 连接成功...");
|
||||
Console.WriteLine($"Minio 已连接...\n初始化桶:{BucketName}");
|
||||
}
|
||||
}
|
|
@ -10,28 +10,31 @@ public static class RedisContext
|
|||
|
||||
public static void AddRedisContext(this WebApplicationBuilder builder)
|
||||
{
|
||||
// 获取端口等配置信息
|
||||
var host = AppSettings.GetConfig("Redis:Host");
|
||||
var port = Convert.ToInt32(AppSettings.GetConfig("Redis:Port"));
|
||||
var defaultDb = Convert.ToInt32(AppSettings.GetConfig("Redis:DefaultDB"));
|
||||
var password = AppSettings.Get<string>("Redis:Password");
|
||||
var timeout = Convert.ToInt32(AppSettings.GetConfig("Redis:AsyncTimeout"));
|
||||
|
||||
// 添加连接地址
|
||||
EndPointCollection.Add(host, port);
|
||||
|
||||
// 初始化连接对象
|
||||
var connect = ConnectionMultiplexer.Connect(new ConfigurationOptions
|
||||
Task.Run(() =>
|
||||
{
|
||||
EndPoints = EndPointCollection,
|
||||
Password = password,
|
||||
DefaultDatabase = defaultDb,
|
||||
AsyncTimeout = timeout
|
||||
// 获取端口等配置信息
|
||||
var host = AppSettings.GetConfig("Redis:Host");
|
||||
var port = Convert.ToInt32(AppSettings.GetConfig("Redis:Port"));
|
||||
var defaultDb = Convert.ToInt32(AppSettings.GetConfig("Redis:DefaultDB"));
|
||||
var password = AppSettings.Get<string>("Redis:Password");
|
||||
var timeout = Convert.ToInt32(AppSettings.GetConfig("Redis:AsyncTimeout"));
|
||||
|
||||
// 添加连接地址
|
||||
EndPointCollection.Add(host, port);
|
||||
|
||||
// 初始化连接对象
|
||||
var connect = ConnectionMultiplexer.Connect(new ConfigurationOptions
|
||||
{
|
||||
EndPoints = EndPointCollection,
|
||||
Password = password,
|
||||
DefaultDatabase = defaultDb,
|
||||
AsyncTimeout = timeout
|
||||
});
|
||||
|
||||
// 创建连接对象
|
||||
RedisDatabase = connect.GetDatabase();
|
||||
|
||||
Console.WriteLine("Redis 连接成功...");
|
||||
});
|
||||
|
||||
// 创建连接对象
|
||||
RedisDatabase = connect.GetDatabase();
|
||||
|
||||
Console.WriteLine("Redis 连接成功...");
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
using Microsoft.AspNetCore.Http;
|
||||
using Minio.DataModel;
|
||||
using Minio.DataModel.Result;
|
||||
|
||||
namespace Bunny.Service.IService;
|
||||
|
@ -9,11 +10,44 @@ public interface IMinioService
|
|||
/// 上传文件
|
||||
/// </summary>
|
||||
/// <param name="file">文件内容</param>
|
||||
void SaveMinioFile(IFormFile? file);
|
||||
/// <param name="filepath"></param>
|
||||
void SaveMinioFile(IFormFile? file, string filepath);
|
||||
|
||||
/// <summary>
|
||||
/// 查询所有的桶
|
||||
/// </summary>
|
||||
/// <returns>桶列表</returns>
|
||||
ListAllMyBucketsResult GetAllMyBuckets();
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件信息
|
||||
/// </summary>
|
||||
/// <param name="filename">文件名</param>
|
||||
/// <param name="bucketName">桶名称</param>
|
||||
/// <returns>ObjectStat</returns>
|
||||
ObjectStat GetObjectStat(string filename, string bucketName);
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件路径
|
||||
/// </summary>
|
||||
/// <param name="filename">文件名</param>
|
||||
/// <param name="bucketName">桶名称</param>
|
||||
/// <returns>文件地址</returns>
|
||||
string GetObjectPath(string filename, string bucketName);
|
||||
|
||||
/// <summary>
|
||||
/// 下载文件
|
||||
/// </summary>
|
||||
/// <param name="filename">文件名</param>
|
||||
/// <param name="bucketName">桶名称</param>
|
||||
/// <returns>下载文件</returns>
|
||||
Task<byte[]> DownloadObject(string filename, string bucketName);
|
||||
|
||||
/// <summary>
|
||||
/// 列出文件夹下所有文件
|
||||
/// </summary>
|
||||
/// <param name="filepath">文件夹</param>
|
||||
/// <param name="bucketName">桶名称</param>
|
||||
/// <returns>所有文件</returns>
|
||||
IAsyncEnumerable<Item> ListObject(string filepath, string bucketName);
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
using Bunny.Common.Connect;
|
||||
using Bunny.Common;
|
||||
using Bunny.Common.Connect;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Minio;
|
||||
using Minio.DataModel;
|
||||
using Minio.DataModel.Args;
|
||||
using Minio.DataModel.Result;
|
||||
|
||||
|
@ -14,25 +16,24 @@ public class MinioService : IMinioService
|
|||
/// 上传文件
|
||||
/// </summary>
|
||||
/// <param name="file">文件内容</param>
|
||||
public async void SaveMinioFile(IFormFile? file)
|
||||
/// <param name="filepath"></param>
|
||||
public async void SaveMinioFile(IFormFile? file, string filepath)
|
||||
{
|
||||
if (file == null)
|
||||
{
|
||||
Console.WriteLine("文件为空");
|
||||
Console.WriteLine("文件未上传");
|
||||
return;
|
||||
}
|
||||
|
||||
var fileName = file.FileName;
|
||||
var contentType = file.ContentType;
|
||||
var putObjectResponse = await _minioContext.PutObjectAsync(new PutObjectArgs()
|
||||
await _minioContext.PutObjectAsync(new PutObjectArgs()
|
||||
.WithBucket(MinioContext.BucketName)
|
||||
.WithObject(fileName)
|
||||
.WithObject($"{filepath}/{fileName}")
|
||||
.WithStreamData(file.OpenReadStream())
|
||||
.WithContentType(contentType)
|
||||
.WithObjectSize(file.Length)
|
||||
);
|
||||
|
||||
Console.WriteLine($"putObjectResponse.ObjectNam---{putObjectResponse.ObjectName}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -41,14 +42,64 @@ public class MinioService : IMinioService
|
|||
/// <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 _minioContext!.ListBucketsAsync().Result;
|
||||
}
|
||||
|
||||
return listBucketsAsync;
|
||||
/// <summary>
|
||||
/// 获取文件信息
|
||||
/// </summary>
|
||||
/// <param name="filename">文件名</param>
|
||||
/// <param name="bucketName">桶名称</param>
|
||||
/// <returns>ObjectStat</returns>
|
||||
public ObjectStat GetObjectStat(string filename, string bucketName)
|
||||
{
|
||||
var objectAsync = _minioContext.GetObjectAsync(new GetObjectArgs()
|
||||
.WithBucket(bucketName)
|
||||
.WithObject(filename)
|
||||
.WithFile(filename)
|
||||
).Result;
|
||||
|
||||
return objectAsync;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件路径
|
||||
/// </summary>
|
||||
/// <param name="filename">文件名</param>
|
||||
/// <param name="bucketName">桶名称</param>
|
||||
/// <returns>文件地址</returns>
|
||||
public string GetObjectPath(string filename, string bucketName)
|
||||
{
|
||||
var url = AppSettings.GetConfig("Minio:Url");
|
||||
return $"{url}/{bucketName}/{filename}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载文件
|
||||
/// </summary>
|
||||
/// <param name="filename">文件名</param>
|
||||
/// <param name="bucketName">桶名称</param>
|
||||
/// <returns>下载文件</returns>
|
||||
public async Task<byte[]> DownloadObject(string filename, string bucketName)
|
||||
{
|
||||
var url = AppSettings.GetConfig("Minio:Url");
|
||||
var downloadUrl = $"http://{url}/{bucketName}/{filename}";
|
||||
using var client = new HttpClient();
|
||||
var responseMessage = await client.GetAsync(downloadUrl);
|
||||
if (!responseMessage.IsSuccessStatusCode) return [];
|
||||
var buffer = await responseMessage.Content.ReadAsByteArrayAsync();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<Item> ListObject(string? filepath, string bucketName)
|
||||
{
|
||||
return _minioContext
|
||||
.ListObjectsEnumAsync(
|
||||
new ListObjectsArgs()
|
||||
.WithBucket(bucketName)
|
||||
.WithPrefix(filepath)
|
||||
.WithVersions(false)
|
||||
.WithRecursive(true)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -19,7 +19,11 @@ 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; });
|
||||
builder.WebHost.ConfigureKestrel((context, options) =>
|
||||
{
|
||||
// 设置文件最大上传大小
|
||||
options.Limits.MaxRequestBodySize = 1024 * 1024 * 100;
|
||||
});
|
||||
// 添加Service服务
|
||||
builder.AddApplicationServices();
|
||||
// 添加后台服务
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using Bunny.Dao.Result;
|
||||
using Bunny.Service.IService;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Minio.DataModel;
|
||||
using Minio.DataModel.Result;
|
||||
|
||||
namespace Bunny.WebApi.Controllers;
|
||||
|
@ -24,10 +25,76 @@ public class MinioController(IMinioService minioService) : ControllerBase
|
|||
/// 上传文件
|
||||
/// </summary>
|
||||
/// <param name="file">文件</param>
|
||||
/// <param name="filepath"></param>
|
||||
[HttpPost]
|
||||
public Result<string> SaveMinioFile(IFormFile? file)
|
||||
public Result<string> SaveMinioFile(IFormFile? file, string filepath = "test")
|
||||
{
|
||||
minioService.SaveMinioFile(file);
|
||||
minioService.SaveMinioFile(file, filepath);
|
||||
return Result<string>.Success("上传成功");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件信息
|
||||
/// </summary>
|
||||
/// <param name="filename">文件名</param>
|
||||
/// <param name="bucketName">桶名称</param>
|
||||
/// <returns>ObjectStat</returns>
|
||||
[HttpGet]
|
||||
public Result<ObjectStat> GetObjectStat(string filename, string bucketName = "csharp-test")
|
||||
{
|
||||
var vo = minioService.GetObjectStat(filename, bucketName);
|
||||
return Result<ObjectStat>.Success(vo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取文件路径
|
||||
/// </summary>
|
||||
/// <param name="filename">文件名</param>
|
||||
/// <param name="bucketName">桶名称</param>
|
||||
/// <returns>文件地址</returns>
|
||||
[HttpGet]
|
||||
public Result<string> GetObjectPath(string filename, string bucketName = "csharp-test")
|
||||
{
|
||||
var vo = minioService.GetObjectPath(filename, bucketName);
|
||||
return Result<string>.Success(vo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载文件
|
||||
/// </summary>
|
||||
/// <param name="filename">文件名</param>
|
||||
/// <param name="bucketName">桶名称</param>
|
||||
/// <returns>下载文件</returns>
|
||||
[HttpGet]
|
||||
public IActionResult DownloadObject(string filename, string bucketName = "csharp-test")
|
||||
{
|
||||
var buffer = minioService.DownloadObject(filename, bucketName).Result;
|
||||
return File(buffer, "application/octet-stream", filename);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查看对象文件展示在网页中
|
||||
/// </summary>
|
||||
/// <param name="filename">文件名</param>
|
||||
/// <param name="bucketName">桶名称</param>
|
||||
/// <returns>下载文件</returns>
|
||||
[HttpGet]
|
||||
public IActionResult ViewObject(string filename, string bucketName = "csharp-test")
|
||||
{
|
||||
var buffer = minioService.DownloadObject(filename, bucketName).Result;
|
||||
return File(buffer, "image/jpeg");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 列出文件夹下所有文件
|
||||
/// </summary>
|
||||
/// <param name="filepath">文件夹</param>
|
||||
/// <param name="bucketName">桶名称</param>
|
||||
/// <returns>所有文件</returns>
|
||||
[HttpGet]
|
||||
public Result<IAsyncEnumerable<Item>> ListObject(string? filepath, string bucketName = "csharp-test")
|
||||
{
|
||||
var vo = minioService.ListObject(filepath, bucketName);
|
||||
return Result<IAsyncEnumerable<Item>>.Success(vo);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue