using Bunny.Common.Context; using Bunny.Common.Exception; using Bunny.Dao.Model.Constant; using Microsoft.AspNetCore.Http; using Microsoft.IdentityModel.Tokens; using Minio.DataModel; using Minio.DataModel.Args; using Minio.DataModel.Result; using Minio.Exceptions; namespace Bunny.Common.Utils; public static class MinioUtil { /// /// 创建新的桶,如果不存在则创建 /// /// 桶名称 public static async Task MakeBucketAsync(string bucketName) { // 判断桶名称是否初始化 bucketName = bucketName.IsNullOrEmpty() ? MinioContext.BucketName : bucketName; // minio客户端 var minioClient = MinioContext.MinioClient; try { var found = await BucketExistsAsync(bucketName); if (!found) await minioClient!.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucketName)); } catch (MinioException e) { Console.WriteLine(e); throw new BunnyException(ExceptionConstant.FileSystemException); } } /// /// 列出所有的桶 /// public static async Task ListBucketsAsync() { var minioClient = MinioContext.MinioClient; try { var listAllMyBucketsResult = await minioClient!.ListBucketsAsync(); return listAllMyBucketsResult; } catch (MinioException e) { Console.WriteLine(e); throw new BunnyException(ExceptionConstant.FileSystemException); } } /// /// 判断桶是否存在 /// /// /// public static async Task BucketExistsAsync(string bucketName) { // 判断桶名称是否初始化 bucketName = bucketName.IsNullOrEmpty() ? MinioContext.BucketName : bucketName; // minio客户端 var minioClient = MinioContext.MinioClient; return await minioClient!.BucketExistsAsync(new BucketExistsArgs().WithBucket(bucketName)); } /// /// 移出桶 /// /// 桶名称 public static async Task RemoveBucketAsync(string bucketName) { // 判断桶名称是否初始化 bucketName = bucketName.IsNullOrEmpty() ? MinioContext.BucketName : bucketName; // minio客户端 var minioClient = MinioContext.MinioClient!; try { // 如果存在则移除 var found = await BucketExistsAsync(bucketName); if (found) await minioClient.RemoveBucketAsync(new RemoveBucketArgs().WithBucket(bucketName)); else Console.WriteLine("桶不存在"); } catch (MinioException e) { Console.WriteLine(e); throw new BunnyException(ExceptionConstant.FileSystemException); } } /// /// 查看桶的版本信息 /// /// 桶名称 public static async Task GetVersioningInfoAsync(string bucketName) { // 判断桶名称是否初始化 bucketName = bucketName.IsNullOrEmpty() ? MinioContext.BucketName : bucketName; // minio客户端 var minioClient = MinioContext.MinioClient!; try { var found = await BucketExistsAsync(bucketName); if (!found) Console.WriteLine("桶不存在"); return await minioClient.GetVersioningAsync(new GetVersioningArgs().WithBucket(bucketName)); } catch (MinioException e) { Console.WriteLine(e); throw new BunnyException(ExceptionConstant.FileSystemException); } } /// /// 上传文件 /// /// 桶名称 /// 文件 public static async Task PutObjectAsync(IFormFile file, string? bucketName = default) { // 判断桶名称是否初始化 bucketName = bucketName.IsNullOrEmpty() ? MinioContext.BucketName : bucketName; // minio客户端 var minioClient = MinioContext.MinioClient!; var guid = Guid.NewGuid().ToString(); var fileName = guid + file.FileName; try { var found = await BucketExistsAsync(bucketName!); if (!found) await MakeBucketAsync(bucketName!); await minioClient.PutObjectAsync(new PutObjectArgs().WithBucket(bucketName).WithObject(fileName) .WithStreamData(file.OpenReadStream()).WithObjectSize(file.Length)); } catch (MinioException e) { Console.WriteLine(e); throw new BunnyException(ExceptionConstant.FileSystemException); } } /// /// 获取文件对象信息 /// /// 桶名称 /// 文件名 public static async Task GetObjectAsync(string filename, string? bucketName = default) { // 判断桶名称是否初始化 bucketName = bucketName.IsNullOrEmpty() ? MinioContext.BucketName : bucketName; // minio客户端 var minioClient = MinioContext.MinioClient!; try { var statObjectArgs = new StatObjectArgs().WithBucket(bucketName).WithObject(filename); await minioClient.StatObjectAsync(statObjectArgs); var getObjectArgs = new GetObjectArgs().WithBucket(bucketName).WithObject(filename).WithFile(filename); return await minioClient.GetObjectAsync(getObjectArgs); } catch (MinioException e) { Console.WriteLine(e); throw new BunnyException(ExceptionConstant.FileSystemException); } } /// /// 桶中内容状态 /// /// 桶名称 /// 文件名 public static async Task StatObject(string filename, string? bucketName = default) { // 判断桶名称是否初始化 bucketName = bucketName.IsNullOrEmpty() ? MinioContext.BucketName : bucketName; // minio客户端 var minioClient = MinioContext.MinioClient!; try { // Get the metadata of the object. var statObjectArgs = new StatObjectArgs() .WithBucket(bucketName) .WithObject(filename); var objectStat = await minioClient.StatObjectAsync(statObjectArgs); return objectStat; } catch (MinioException e) { Console.WriteLine(e); throw new BunnyException(ExceptionConstant.FileSystemException); } } }