minio使用方式
minio是一款开源的对象存储服务器,兼容亚马逊的S3协议 , 对Kubernetes能够友好的支持,专为AI等云原生工作负载而设计
一.Docker安装
``` docker pull minio/minio docker run -p 9999:9000 -p 9001:9001 --name minio \ -d --restart=always \ -e "MINIO_ROOT_USER=abc" \ -e "MINIO_ROOT_PASSWORD=mr123321" \ -v /mnt/data:/data \ -v /mnt/config:/root/.minio minio/minio server /data \ -v /etc/localtime:/etc/localtime:ro \ --console-address ":9001"
```
说明:
--console-address是控制台端口
--address是api端口(默认9000)
二,Nginx使其支持SSL
server { listen 443 ssl; server_name XXXX.cn; ssl on; ssl_certificate /etc/nginx/cert/7891070_XXXX.cn.pem; ssl_certificate_key /etc/nginx/cert/7891070_XXXX.cn.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { proxy_pass http://127.0.0.1:9999;
# 此处有坑,如果不写默认是1.0,不支持文件分块传递 proxy_http_version 1.1; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; } }
proxy_http_version 1.1;必须声明,否则minio获取文件时报错“The request signature we calculated does not match the signature you pr...”
三..NETCORE调用
public class MinioHelper { private const string accessKey = "abc"; private const string secretKey = "mr123321"; private const string bucketName = "minio"; /// <summary> /// 内部连接端 /// </summary> private static MinioClient inMinio = null; #region 单例构造 private static MinioHelper _instance; private MinioHelper() { } public static MinioHelper Instance() { if (_instance == null) { _instance = new MinioHelper(); var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); var config = builder.Build(); inMinio = new MinioClient(config.GetConnectionString("endpoint"), accessKey, secretKey ); } return _instance; } #endregion public async Task PutFile(Stream filestream, string filePath, string contentType = null) { await this.checkBucket(inMinio); // Upload a file to bucket. await inMinio.PutObjectAsync(bucketName, filePath, filestream, filestream.Length, contentType); } public async Task<byte[]> GetFileMs(string filePath) { await this.checkBucket(inMinio); using (MemoryStream ms = new MemoryStream()) { await inMinio.GetObjectAsync(bucketName, filePath, (stream) => { stream.CopyTo(ms); }); return ms.GetBuffer(); } } /// <summary> /// 获取文件是否存在 /// </summary> /// <param name="filePath"></param> /// <returns></returns> public async Task<bool> GetFileStat(string filePath) { try { ObjectStat stat = await inMinio.StatObjectAsync(bucketName, filePath.Replace("//", "/")); return Path.GetFileName(stat.ObjectName) == Path.GetFileName(filePath); } catch (MinioException) { return false; } } /// <summary> /// 删除文件 /// </summary> /// <param name="filePath"></param> /// <returns></returns> public async Task RemoveFile(string filePath) { await this.checkBucket(inMinio); // Upload a file to bucket. await inMinio.RemoveObjectAsync(bucketName, filePath); } /// <summary> /// 获取存储桶中的文件地址,有效期1小时 /// </summary> /// <param name="filePath"></param> /// <returns></returns> public async Task<String> GetFileUrl(string filePath) { await this.checkBucket(inMinio); String url = await inMinio.PresignedGetObjectAsync(bucketName, filePath, 60 * 60 * 1); return url; } /// <summary> /// 检查存储桶是否存在 /// </summary> /// <returns></returns> private async Task checkBucket(MinioClient minio) { bool found = await minio.BucketExistsAsync(bucketName); if (!found) { await minio.MakeBucketAsync(bucketName); } } }