MinIO自建对象存储
1、dock-compose
使用dock-compose拉取
minio:image: "minio/minio"container_name: minioports:- "9000:9000"- "9001:9001"volumes:- "./minio/data1:/data1"- "./minio/data2:/data2"restart: on-failurecommand: server --console-address ":9001" http://minio/data{1...2}environment:- MINIO_ROOT_USER=user007 # 管理账户- MINIO_ROOT_PASSWORD=User1234 # 密码
2、登录后台
http://localhost:9001 ,新建buckets,设置Access Policy为public
3、json配置(建议启用ssl)
"MinIO": {"Endpoint": "localhost:9000","AccessKey": "2ZTepad5iCXIYaey16KU","SecretKey": "b2NJRawoOyS6vx0qxvAW66YVEFReeXY375GMso1J","WithSSL": false}
4、注入
public static void AddCustomOSS(this WebApplicationBuilder builder){var minio = builder.Configuration.GetSection("MinIO");var withSSL = bool.Parse(minio["WithSSL"]);builder.Services.AddMinio(configureClient => configureClient.WithEndpoint(minio["Endpoint"]).WithCredentials(minio["AccessKey"], minio["SecretKey"]).WithSSL(withSSL).Build());}
5、上传
public async Task UploadFile(IFormFile file)
{
try
{
//获取上传存储地址和文件名
var fileName = $“{Guid.NewGuid():N}{Path.GetExtension(file.FileName)}”;
var objectName = $“/{DateTime.Now:yyyy/MM/dd}/{fileName}”;
//获取上传文件输入流
var stream = file.OpenReadStream();
//上传文件
var putObjectArgs = new PutObjectArgs()
.WithBucket(bucketName)
.WithObject(objectName)
.WithStreamData(stream)
.WithObjectSize(file.Length)
.WithContentType(file.ContentType);
var result = await _minioClient.PutObjectAsync(putObjectArgs).ConfigureAwait(false);
return result.ObjectName;
}
catch (MinioException ex)
{
throw new MinioException(“上传失败”, ex);
}
}
6、访问
http://localhost:9000/{BucketName}/{5d61907cfb974a488f5b7d7e9b4ef7b8.png}