MinIOHelper
using Microsoft.Extensions.Configuration; using Minio; using Minio.DataModel; using Minio.Exceptions; using System; using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; namespace Chocolate.Common.Helper { public class MinioHelper { private readonly IConfiguration _configration; public MinioHelper(IConfiguration configration) { _configration = configration; } /// <summary> /// 上传文件 /// </summary> /// <param name="bucketName">存储桶名称</param> /// <param name="objectName">对象名称</param> /// <param name="memoryStream">文件数据流</param> /// <param name="withSSL">是否启用SSL/TLS加密</param> /// <returns></returns> public async Task UploadFile(string bucketName, string objectName, Stream memoryStream, bool withSSL = false) { string endPoint = _configration["MinIO:EndPoint"]; // 端点 string accessKey = _configration["MinIO:AccessKey"]; // 访问凭证 string secretKey = _configration["MinIO:SecretKey"]; // 访问凭证 // 创建MinIO客户端 MinioClient client = Create(endPoint, accessKey, secretKey, withSSL); await MinioHelper.PutObjectAsync(client, bucketName, objectName, memoryStream, memoryStream.Length); } /// <summary> /// 创建MinIO客户端 /// </summary> /// <param name="endPoint"></param> /// <param name="accessKey"></param> /// <param name="secretKey"></param> /// <param name="withSSL"></param> /// <returns></returns> public static MinioClient Create(string endPoint, string accessKey, string secretKey, bool withSSL = false) { MinioClient client = new MinioClient() .WithEndpoint(endPoint) .WithCredentials(accessKey, secretKey); // 启用HTTPS安全连接 if (withSSL) { client = client.WithSSL(); } return client.Build(); } /// <summary> /// 通过Stream上传对象 /// </summary> /// <param name="minio">连接实例</param> /// <param name="bucketName">存储桶名称</param> /// <param name="objectName">存储桶里的对象名称</param> /// <param name="data">要上传的Stream对象</param> /// <param name="size">流的大小</param> /// <param name="contentType">文件的Content type,默认是"application/octet-stream"</param> /// <param name="metaData">元数据头信息的Dictionary对象,默认是null</param> /// <returns></returns> public async static Task PutObjectAsync(MinioClient minio, string bucketName, string objectName, Stream data, long size, string contentType = "application/octet-stream", Dictionary<string, string> metaData = null) { // 检查存储桶是否存在 CheckBucket(minio, bucketName); try { PutObjectArgs args = new PutObjectArgs() .WithBucket(bucketName) // 设置存储桶 .WithObject(objectName) // 设置对象名称 .WithStreamData(data) // 设置数据流 .WithObjectSize(size) // 设置对象大小 .WithContentType(contentType) // 设置内容类型 .WithHeaders(metaData) // 设置元数据 .WithServerSideEncryption(null); // 禁用服务器端加密 await minio.PutObjectAsync(args); } catch (MinioException e) { throw new Exception(e.Message); } } /// <summary> /// 校验桶是否存在,如果不存在则报错 /// <example> /// 调用示例 /// <code> /// bool exists = MinioHelper.BucketExists(minio, buckName); /// </code> /// </example> /// </summary> /// <param name="minio"></param> /// <param name="bucketName"></param> /// <exception cref="Exception"></exception> private static void CheckBucket(MinioClient minio, string bucketName) { bool found = BucketExists(minio, bucketName); if (!found) { throw new Exception(string.Format("存储桶[{0}]不存在", bucketName)); } } /// <summary> /// 检查存储桶是否存在 /// </summary> /// <example> /// <code> /// var data = MinioHelper.ListBuckets(minio); /// </code> /// </example> /// <param name="minio">连接实例</param> /// <param name="bucketName">存储桶名称</param> /// <returns></returns> public static bool BucketExists(MinioClient minio, string bucketName, CancellationToken cancellationToken = default(CancellationToken)) { try { BucketExistsArgs args = new BucketExistsArgs() .WithBucket(bucketName); Task<bool> bucketExistTask = minio.BucketExistsAsync(args); Task.WaitAll(bucketExistTask); return bucketExistTask.Result; } catch (Exception e) { throw new Exception(e.Message); } } }
收余恨、免娇嗔、且自信、改性情,休恋逝水,苦海回身,早悟兰因。

浙公网安备 33010602011771号