增加minio配置
appsetting中
"MinioUrl": "MinioClientUrl",
"MinioUser": "UserName",
"MinioPassword": "Password",
"MinioBucketName": "bucketName",
Stratup
public Startup(IConfiguration configuration)
{
Configuration = configuration;
MinioUrl = Configuration.GetConnectionString("MinioUrl");
MinioUser = Configuration.GetConnectionString("MinioUser");
MinioPassword = Configuration.GetConnectionString("MinioPassword");
MinioBucketName = Configuration.GetConnectionString("MinioBucketName");
}
定义minio服务类
新建MinioClient类
using Microsoft.AspNetCore.Http;
using Minio;
using ServiceReference;
using System;
using System.IO;
using System.Threading.Tasks;
namespace GreeEquipmentConSysAPI.Commons
{
public class MinioServer
{
///
/// 定义MinIO客户端对象
///
private MinioClient minio;
///
/// 构造函数中注入实例
///
public MinioServer()
{
minio = new MinioClient(Startup.MinioUrl, Startup.MinioUser, Startup.MinioPassword);
}
///
/// 上传文件到MinIO
///
public async Task UploadFile(Stream stream, string bucketName, string objectName, string path)
{
return await Task.Run(async () =>
{
var retMsg = string.Empty;
try
{
//检查是否存在桶
bool existBucket = await minio.BucketExistsAsync(bucketName);
if (!existBucket)
{
//创建桶
await minio.MakeBucketAsync(bucketName);
//设置桶的访问权限(读、写、读和写)
await SetPolicyAsync(bucketName, 3);
}
//检查设备令牌是否重复
//List listfile = getPreMinioObjectNameList(edgeName + "/" + acesst);
//if (listfile.Count == 0)
//{
//上传文件到桶中
await minio.PutObjectAsync(bucketName, "/" + path + "/" + objectName, stream, stream.Length);
retMsg = "success";
//}
//else
//{
// retMsg = "设备令牌重复";
//}
}
catch (Exception e)
{
retMsg = "文件上传错误:" + e.Message;
}
return retMsg;
});
}
///
/// 从minio删除文件
///
///
public async Task RemoveFile(string bucketName, string objectName)
{
return await Task.Run(async () =>
{
var retMsg = string.Empty;
try
{
//检查是否存在桶
bool existBucket = await minio.BucketExistsAsync(bucketName);
if (!existBucket)
{
return "minio桶不存在!";
}
await minio.RemoveObjectAsync(bucketName, objectName);
retMsg = "success";
}
catch (Exception e)
{
retMsg = "文件删除错误:" + e.Message;
}
return retMsg;
});
}
///
/// 删除文件
///
/// minio桶名称
/// 存储对象名称
///
public async Task downLoadFile(string bucketName, string objectName)
{
var memoryStream = new MemoryStream();
try
{
await minio.GetObjectAsync(bucketName, objectName, (stream) => { stream.CopyTo(memoryStream); });
memoryStream.Position = 0;
}
catch (Exception e)
{
return "未找到文件,下载失败" + e.Message;
}
return memoryStream;
}
///
/// 检查minio桶是否存在
///
/// 存储桶名称
///
public async Task BucketExistsAsync(string bucketName)
{
bool existBucket = await minio.BucketExistsAsync(bucketName);
return existBucket;
}
///
/// 设置桶的访问策略(读、写、读和写)
///
///
///
///
private async Task SetPolicyAsync(string bucketName, int policyType = 1)
{
//设置桶的访问权限(读、写、读和写)
var policyRead = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetBucketLocation\"],\"Resource\":[\"arn:aws:s3:::BUCKETNAME\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:ListBucket\"],\"Resource\":[\"arn:aws:s3:::BUCKETNAME\"],\"Condition\":{\"StringEquals\":{\"s3:prefix\":[\"BUCKETPREFIX\"]}}},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetObject\"],\"Resource\":[\"arn:aws:s3:::BUCKETNAME/BUCKETPREFIX*\"]}]}";
var policyWrite = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucketMultipartUploads\"],\"Resource\":[\"arn:aws:s3:::BUCKETNAME\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:AbortMultipartUpload\",\"s3:DeleteObject\",\"s3:ListMultipartUploadParts\",\"s3:PutObject\"],\"Resource\":[\"arn:aws:s3:::BUCKETNAME/BUCKETPREFIX*\"]}]}";
var policyReadWrite = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucketMultipartUploads\"],\"Resource\":[\"arn:aws:s3:::BUCKETNAME\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:ListBucket\"],\"Resource\":[\"arn:aws:s3:::BUCKETNAME\"],\"Condition\":{\"StringEquals\":{\"s3:prefix\":[\"BUCKETPREFIX\"]}}},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:PutObject\",\"s3:AbortMultipartUpload\",\"s3:DeleteObject\",\"s3:GetObject\",\"s3:ListMultipartUploadParts\"],\"Resource\":[\"arn:aws:s3:::BUCKETNAME/BUCKETPREFIX*\"]}]}";
var policySet = policyType == 1 ? policyRead :
policyType == 2 ? policyWrite : policyReadWrite;
await minio.SetPolicyAsync(bucketName, policySet.Replace("BUCKETNAME", bucketName).Replace("BUCKETPREFIX", "*.*"));
}
}
}
使用
public class MachineDataController:ControllerBase
{
///
/// 定义MinIO客户端对象
///
private MinioServer minioServer = new MinioServer();
///
/// 构造函数中注入实例
///
public MachineDataController()
{
minio = new MinioClient(Startup.MinioUrl, Startup.MinioUser, Startup.MinioPassword);
}
public void function() {
string res = string.Empty;
//上传文件
res = minioServer.UploadFile(pdfFile[0].OpenReadStream(), "greebaseapprovalpdfs", departName + "_" + year + "_" + month + ".pdf", departName).Result;
//下载文件
var memoryStream = new MemoryStream();
memoryStream = await minioServer.downLoadFile("greebaseapprovalpdfs", "/" + departName + "/" + fileName);
return File(memoryStream, "application/pdf");
//删除文件
string resMsg = minioServer.RemoveFile("greebaseapprovalpdfs", "/" + departName + "/" + departName + "_" + year + "_" + month + ".pdf").Result;
// 检查桶是否存在
bool existBucket = await minioServer.BucketExistsAsync("greebaseapprovalpdfs");
}
}