MasstransferExporter/MasstransferInfrastructure/Minio/MinioHelper.cs

117 lines
3.4 KiB
C#
Raw Normal View History

2024-07-09 08:27:33 +00:00
using MasstransferCommon.Model.Entity;
using MasstransferInfrastructure.Database.Sqlite;
using Minio;
2024-06-19 11:28:48 +00:00
using Minio.DataModel.Args;
namespace MasstransferCommunicate.Minio;
/// <summary>
/// Minio工具类
/// </summary>
public class MinioHelper
{
private readonly IMinioClient _client;
2024-07-09 08:27:33 +00:00
private static MinioHelper? _instance;
private static readonly object Lock = new();
private static SqliteHelper _sqliteHelper = SqliteHelper.GetInstance();
private MinioHelper(string endpoint, string accessKey, string secretKey)
2024-06-19 11:28:48 +00:00
{
_client = new MinioClient()
2024-07-17 06:28:24 +00:00
.WithSSL()
2024-06-19 11:28:48 +00:00
.WithEndpoint(endpoint)
.WithCredentials(accessKey, secretKey)
.Build();
}
2024-07-09 08:27:33 +00:00
public static MinioHelper GetInstance()
{
lock (Lock)
{
var minio = _sqliteHelper.Query<MinioParams>("select * from minio_params limit 1").FirstOrDefault();
_instance ??= new MinioHelper(minio.MinioEndpoint, minio.MinioAccessKey, minio.MinioSecretKey);
return _instance;
}
}
2024-06-19 11:28:48 +00:00
/// <summary>
/// 判断bucket 是否存在
/// </summary>
/// <param name="bucketName"></param>
/// <returns></returns>
public async Task<bool> BucketExistsAsync(string bucketName)
{
return await _client.BucketExistsAsync(new BucketExistsArgs().WithBucket(bucketName));
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="bucketName"></param>
/// <param name="fileName"></param>
/// <param name="filePath"></param>
/// <returns></returns>
public async Task<string> UploadFileAsync(string bucketName, string fileName, string filePath)
{
2024-07-18 02:46:12 +00:00
try
2024-06-19 11:28:48 +00:00
{
2024-07-18 02:46:12 +00:00
if (!await BucketExistsAsync(bucketName))
{
await _client.MakeBucketAsync(new MakeBucketArgs().WithBucket(bucketName));
}
2024-06-19 11:28:48 +00:00
2024-07-18 02:46:12 +00:00
await _client.PutObjectAsync(new PutObjectArgs()
.WithBucket(bucketName)
.WithObject(fileName)
.WithFileName(filePath));
2024-06-19 11:28:48 +00:00
2024-07-18 02:46:12 +00:00
Console.WriteLine("文件上传成功");
2024-07-17 06:22:08 +00:00
2024-07-18 02:46:12 +00:00
return $"{bucketName}/{fileName}";
}
catch (Exception e)
{
Console.WriteLine($"文件上传失败: {e}");
return "";
}
2024-06-19 11:28:48 +00:00
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="bucketName"></param>
/// <param name="fileName"></param>
/// <param name="filePath"></param>
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));
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="bucketName"></param>
/// <param name="fileName"></param>
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));
}
}