using Minio; using Minio.DataModel.Args; namespace MasstransferCommunicate.Minio; /// /// Minio工具类 /// public class MinioHelper { private readonly IMinioClient _client; public MinioHelper(string endpoint, string accessKey, string secretKey) { _client = new MinioClient() .WithEndpoint(endpoint) .WithCredentials(accessKey, secretKey) .Build(); } /// /// 判断bucket 是否存在 /// /// /// public async Task BucketExistsAsync(string bucketName) { return await _client.BucketExistsAsync(new BucketExistsArgs().WithBucket(bucketName)); } /// /// 上传文件 /// /// /// /// /// public async Task UploadFileAsync(string bucketName, string fileName, string filePath) { if (!await BucketExistsAsync(bucketName)) { await _client.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucketName)); } await _client.PutObjectAsync(new PutObjectArgs() .WithBucket(bucketName) .WithObject(fileName) .WithFileName(filePath)); return $"{bucketName}/{fileName}"; } /// /// 下载文件 /// /// /// /// public async Task DownloadFileAsync(string bucketName, string fileName, string filePath) { if (!await BucketExistsAsync(bucketName)) { await _client.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucketName)); } await _client.GetObjectAsync(new GetObjectArgs() .WithBucket(bucketName) .WithObject(fileName) .WithFile(filePath)); } /// /// 删除文件 /// /// /// public async Task DeleteFileAsync(string bucketName, string fileName) { if (!await BucketExistsAsync(bucketName)) { await _client.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucketName)); } await _client.RemoveObjectAsync(new RemoveObjectArgs() .WithBucket(bucketName) .WithObject(fileName)); } }