最近想配置一款可以存储文件的服务器,如果使用阿里云OSS、七牛云、腾讯云需要收取比较高的费用,为了节约成本,考虑使用免费开源的软件进行部署,然后对fastDFS和MiniO进行了对比:
一:安装部署(运维)复杂度
fastDFS分布式服务部署完成,需要具备以下的知识:
linux基础的目录操作
常用的分布式主从原理
C语言代码的编译
nginx安装部署
nginx插件的使用(防盗链)
如果仅仅是上面的这些基础知识,安排几个程序员学一学还好说。主要是fastdfs的部署结构之复杂,如果我长时间不回顾,自己都会忘了这复杂的架构是怎么回事。
当我看到MinIO的安装过程之后,以及分布式的部署命令之后(分布式MinIO快速入门),放弃fastDFS的决心就已经做出了一大半。
说白了:FastDFS的部署不过是零件的组装过程,需要你去理解fastDFS的架构设计,才能够正确的安装部署。MinIO在安装的过程是黑盒的,你不用去深入关注它的架构,也不需要你进行零件组装,基本上可以做到开箱即用。普通的技术人员就能够参与后期运维。
二:文档
我觉得从我知道fastDFS开始,也有十年了。竟然没有官方文档,所有的文档全是某某公司的自己总结的文档,或者是某某网友自己总结的文档。
从这点上看fastDFS真的是一败涂地,当然阿里余庆大神在做这个项目的时候可能也没有考虑到后来会有这么多人用。即使用的人多了,在余庆大神眼里可能觉得这只是自己开发的一个小玩具,没有继续深入运营的必要。
三:开源项目运营组织
fastdfs是阿里余庆做的一个个人项目,在一些互联网创业公司中有应用,没有官网,不活跃,6个contributors。目前已经很少做更新。
MinIO目前是由2014年在硅谷创立的公司MinIO.Inc运营的开源项目,社区论坛的活跃度目前也非常的不错。
四:UI界面
我们都知道fastDFS默认是不带UI界面的,MinIO有比较丰富的UI界面
五:性能
MinIO号称是世界上速度最快的对象存储服务器。在标准硬件上,对象存储的读/写速度最高可以达到183 GB/s和171 GB/s。关于fastDFS我曾经单线程测试写了20万个文件,总共200G,大约用时10个小时。总体上是很难达到MinIO“号称的”以G为单位的每秒读写速度。
六:容器化支持
MinIO提供了与k8s、etcd、docker等容器化技术深度集成方案,可以说就是为了云环境而生的。这点是FastDFS不具备的。
七:丰富的SDK支持
fastDFS目前提供了 C 和 Java SDK ,以及 PHP 扩展 SDK。下图是MinIO提供的SDK支持,MinIO几乎提供了所有主流开发语言的SDK以及文档。同志们,重要的是文档。
八:AWS S3标准兼容
Amazon的S3 API是对象存储领域的事实标准。MinIO是S3兼容性的事实上的标准,是第一个采用API和第一个添加对S3 Select支持的标准之一。包括微软Azure在内的750多家公司使用MinIO的S3网关,这一数字超过了业内其他公司的总和。
文档地址:MinIO | The MinIO Quickstart Guide
安装包下载地址: https://dl.minio.io/server/minio/release/windows-amd64/minio.exe
一、安装部署
1、下载安装包(下载地址在上面)
2、放到磁盘中,后面会以这个文件进行服务器安装
3、磁盘新建一个目录用于存放上传文件,比如我创建的为:Data
4、运行控制台程序:切换到安装包所在目录(后面部分为存储路径), 输入命令:minio.exe server C:\minio\Data
5、打开浏览器,访问:http://127.0.0.1:9000 用户名和密码默认为:minioadmin,主界面如下
二、注册成window服务
1. 下载winsw,下载地址:Releases · winsw/winsw · GitHub
CSDN下载地址:https://download.csdn.net/download/feritylamb/86394140
2. 将WinSW-net461.exe复制到自定义的目录,并重命名为自己想命名的服务名称minio-server.exe
3. 同目录下创建minio-server.xml。特别注意,xml和exe必须同名
4. 配置minio-server.xml文件
5.新建run.bat文件,内容如下:
set MINIO_ACCESS_KEY=admin
set MINIO_SECRET_KEY=12345678
minio.exe server C:\minio\Data
带密码带自己定义端口9991启动命令:
set MINIO_ACCESS_KEY=admin
set MINIO_SECRET_KEY=12345678
minio.exe server --address:9991 C:\minio\Data
5. 使用minio-server.exe install安装服务
安装服务命令:minio-server.exe install
启动服务命令:minio-server.exe start
停止服务命令:minio-server.exe stop
移除服务命令:sc delete minio-server
xml配置文件如下:
<service>
<id>minio-server</id>
<name>minio-server</name>
<description>minio文件存储服务器</description>
<!-- 可设置环境变量 -->
<env name="HOME" value="%BASE%"/>
<executable>%BASE%\minio.exe</executable>
<arguments>server "%BASE%\data"</arguments>
<!-- <logmode>rotate</logmode> -->
<logpath>%BASE%\logs</logpath>
<log mode="roll-by-size-time">
<sizeThreshold>10240</sizeThreshold>
<pattern>yyyyMMdd</pattern>
<autoRollAtTime>00:00:00</autoRollAtTime>
<zipOlderThanNumDays>5</zipOlderThanNumDays>
<zipDateFormat>yyyyMMdd</zipDateFormat>
</log>
</service>
注:如果启动不成功,可以把以上description里面的中文去掉
三、C#文件上传下载文章来源:https://www.toymoban.com/news/detail-786789.html
public async void testupload()
{
try
{
MinioClient minio = new MinioClient("127.0.0.1:9000", "minioadmin", "minioadmin");
//桶名称
string buckName = "202110001";
//判断桶是否存在,如果不存在则创建桶,否则上传文件会异常
var exists = await MinioHelper.BucketExists(minio, buckName);
if (!exists)
{
//创建桶
await MinioHelper.MakeBucket(minio, buckName);
}
//上传文件(桶下面可以自定义文件夹。如:1027/20211027001.jpg 则会创建一个1027文件夹)
var result = await MinioHelper.FPutObject(minio, buckName, "1027/20211027001.png", "E:\\202110271417.png");
//下载文件地址:192.168.90.128:9000+桶名称+文件名称
}
catch (Exception ex)
{
string str = ex.Message;
}
}
四、minio文件操作帮助类文章来源地址https://www.toymoban.com/news/detail-786789.html
public class MinioHelper
{
#region 操作存储桶
/// <summary>创建存储桶
/// 创建存储桶
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="loc">可选参数</param>
/// <returns></returns>
public async static Task<bool> MakeBucket(MinioClient minio, string bucketName, string loc = "us-east-1")
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
throw new Exception(string.Format("存储桶[{0}]已存在", bucketName));
}
else
{
await minio.MakeBucketAsync(bucketName, loc);
flag = true;
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return flag;
}
/// <summary>列出所有的存储桶
/// 列出所有的存储桶
/// </summary>
/// <param name="minio">连接实例</param>
/// <returns></returns>
public async static Task<Tuple<bool, Minio.DataModel.ListAllMyBucketsResult>> ListBuckets(MinioClient minio)
{
bool flag = false;
var list = new Minio.DataModel.ListAllMyBucketsResult();
try
{
list = await minio.ListBucketsAsync();
flag = true;
//foreach (var bucket in list.Buckets)
//{
// Console.WriteLine($"{bucket.Name} {bucket.CreationDateDateTime}");
//}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return Tuple.Create(flag, list);
}
/// <summary>检查存储桶是否存在
/// 检查存储桶是否存在
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <returns></returns>
public async static Task<bool> BucketExists(MinioClient minio, string bucketName)
{
bool flag = false;
try
{
flag = await minio.BucketExistsAsync(bucketName);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return flag;
}
/// <summary>删除一个存储桶
/// 删除一个存储桶
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <returns></returns>
public async static Task<bool> RemoveBucket(MinioClient minio, string bucketName)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
await minio.RemoveBucketAsync(bucketName);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return flag;
}
/// <summary>列出存储桶里的对象
/// 列出存储桶里的对象
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="prefix">对象的前缀</param>
/// <param name="recursive">true代表递归查找,false代表类似文件夹查找,以'/'分隔,不查子文件夹</param>
public static Tuple<bool, IObservable<Item>> ListObjects(MinioClient minio, string bucketName, string prefix = null, bool recursive = true)
{
bool flag = false;
IObservable<Item> observable = null;
try
{
var found = minio.BucketExistsAsync(bucketName);
if (found.Result)
{
observable = minio.ListObjectsAsync(bucketName, prefix, recursive);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
//IDisposable subscription = observable.Subscribe(
// item => Console.WriteLine($"Object: {item.Key}"),
// ex => Console.WriteLine($"OnError: {ex}"),
// () => Console.WriteLine($"Listed all objects in bucket {bucketName}\n"));
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return Tuple.Create(flag, observable);
}
/// <summary>列出存储桶中未完整上传的对象
/// 列出存储桶中未完整上传的对象
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="prefix">对象的前缀</param>
/// <param name="recursive">true代表递归查找,false代表类似文件夹查找,以'/'分隔,不查子文件夹</param>
/// <returns></returns>
public static Tuple<bool, IObservable<Upload>> ListIncompleteUploads(MinioClient minio, string bucketName, string prefix = null, bool recursive = true)
{
bool flag = false;
IObservable<Upload> observable = null;
try
{
var found = minio.BucketExistsAsync(bucketName);
if (found.Result)
{
observable = minio.ListIncompleteUploads(bucketName, prefix, recursive);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
//IDisposable subscription = observable.Subscribe(
// item => Console.WriteLine($"OnNext: {item.Key}"),
// ex => Console.WriteLine($"OnError: {ex.Message}"),
// () => Console.WriteLine($"Listed the pending uploads to bucket {bucketName}"));
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return Tuple.Create(flag, observable);
}
#endregion
#region 存储桶策略
/// <summary>
/// 获取存储桶或者对象前缀的访问权限
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <returns></returns>
public async static Task<Tuple<bool, string>> GetPolicy(MinioClient minio, string bucketName)
{
bool flag = false;
string policyJson = string.Empty;
try
{
var found = minio.BucketExistsAsync(bucketName);
if (found.Result)
{
policyJson = await minio.GetPolicyAsync(bucketName);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return Tuple.Create(flag, policyJson);
}
/// <summary>
/// 针对存储桶和对象前缀设置访问策略
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <returns></returns>
public async static Task<bool> SetPolicy(MinioClient minio, string bucketName)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
string policyJson = $@"{{""Version"":""2012-10-17"",""Statement"":[{{""Action"":[""s3:GetBucketLocation""],""Effect"":""Allow"",""Principal"":{{""AWS"":[""*""]}},""Resource"":[""arn:aws:s3:::{bucketName}""],""Sid"":""""}},{{""Action"":[""s3:ListBucket""],""Condition"":{{""StringEquals"":{{""s3:prefix"":[""foo"",""prefix/""]}}}},""Effect"":""Allow"",""Principal"":{{""AWS"":[""*""]}},""Resource"":[""arn:aws:s3:::{bucketName}""],""Sid"":""""}},{{""Action"":[""s3:GetObject""],""Effect"":""Allow"",""Principal"":{{""AWS"":[""*""]}},""Resource"":[""arn:aws:s3:::{bucketName}/foo*"",""arn:aws:s3:::{bucketName}/prefix/*""],""Sid"":""""}}]}}";
await minio.SetPolicyAsync(bucketName, policyJson);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return flag;
}
#endregion
#region 存储桶通知
/// <summary>
/// 获取存储桶的通知配置
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <returns></returns>
private async static Task<Tuple<bool, string>> GetBucketNotification(MinioClient minio, string bucketName)
{
bool flag = false;
string Ret = string.Empty;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
BucketNotification notifications = await minio.GetBucketNotificationsAsync(bucketName);
Ret = notifications.ToXML();
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (MinioException e)
{
throw new Exception(e.Message);
}
return Tuple.Create(flag, Ret);
}
/// <summary>
/// 给存储桶设置通知
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <returns></returns>
private async static Task<bool> SetBucketNotification(MinioClient minio, string bucketName)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
BucketNotification notification = new BucketNotification();
Arn topicArn = new Arn("aws", "sns", "us-west-1", "412334153608", "topicminio");
TopicConfig topicConfiguration = new TopicConfig(topicArn);
List<EventType> events = new List<EventType>() { EventType.ObjectCreatedPut, EventType.ObjectCreatedCopy };
topicConfiguration.AddEvents(events);
topicConfiguration.AddFilterPrefix("images");
topicConfiguration.AddFilterSuffix("jpg");
notification.AddTopic(topicConfiguration);
QueueConfig queueConfiguration = new QueueConfig("arn:aws:sqs:us-west-1:482314153608:testminioqueue1");
queueConfiguration.AddEvents(new List<EventType>() { EventType.ObjectCreatedCompleteMultipartUpload });
notification.AddQueue(queueConfiguration);
await minio.SetBucketNotificationsAsync(bucketName, notification);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (MinioException e)
{
throw new Exception(e.Message);
}
return flag;
}
/// <summary>
/// 删除存储桶上所有配置的通知
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <returns></returns>
private async static Task<bool> RemoveAllBucketNotifications(MinioClient minio, string bucketName)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
await minio.RemoveAllBucketNotificationsAsync(bucketName);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (MinioException e)
{
throw new Exception(e.Message);
}
return flag;
}
#endregion
#region 操作文件对象
/// <summary>
/// 从桶下载文件到本地
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="objectName">存储桶里的对象名称</param>
/// <param name="fileName">本地路径</param>
/// <param name="sse"></param>
/// <returns></returns>
public async static Task<bool> FGetObject(MinioClient minio, string bucketName, string objectName, string fileName, ServerSideEncryption sse = null)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
if (File.Exists(fileName))
{
File.Delete(fileName);
}
await minio.GetObjectAsync(bucketName, objectName, fileName, sse).ConfigureAwait(false);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (MinioException e)
{
throw new Exception(e.Message);
}
return flag;
}
/// <summary>
/// 上传本地文件至存储桶
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="objectName">存储桶里的对象名称</param>
/// <param name="fileName">本地路径</param>
/// <returns></returns>
public async static Task<bool> FPutObject(MinioClient minio, string bucketName, string objectName, string fileName)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
await minio.PutObjectAsync(bucketName, objectName, fileName, contentType: "application/octet-stream");
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (MinioException e)
{
throw new Exception(e.Message);
}
return flag;
}
#endregion
#region Presigned操作
/// <summary>生成一个给HTTP GET请求用的presigned URL。浏览器/移动端的客户端可以用这个URL进行下载,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
/// 生成一个给HTTP GET请求用的presigned URL。浏览器/移动端的客户端可以用这个URL进行下载,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="objectName">存储桶里的对象名称</param>
/// <param name="expiresInt">失效时间(以秒为单位),默认是7天,不得大于七天</param>
/// <param name="reqParams">额外的响应头信息,支持response-expires、response-content-type、response-cache-control、response-content-disposition</param>
/// <returns></returns>
public async static Task<Tuple<bool, string>> PresignedGetObject(MinioClient minio, string bucketName, string objectName, int expiresInt = 1000)
{
bool flag = false;
string Ret = string.Empty;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
var reqParams = new Dictionary<string, string> { { "response-content-type", "application/json" } };
string presignedUrl = await minio.PresignedGetObjectAsync(bucketName, objectName, expiresInt, reqParams);
Ret = presignedUrl;
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return Tuple.Create(flag, Ret);
}
/// <summary>生成一个给HTTP PUT请求用的presigned URL。浏览器/移动端的客户端可以用这个URL进行上传,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
/// 生成一个给HTTP PUT请求用的presigned URL。浏览器/移动端的客户端可以用这个URL进行上传,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="objectName">存储桶里的对象名称</param>
/// <param name="expiresInt">失效时间(以秒为单位),默认是7天,不得大于七天</param>
/// <returns></returns>
public async static Task<Tuple<bool, string>> PresignedPutObject(MinioClient minio, string bucketName, string objectName, int expiresInt = 1000)
{
bool flag = false;
string Ret = string.Empty;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
string presignedUrl = await minio.PresignedPutObjectAsync(bucketName, objectName, expiresInt);
Ret = presignedUrl;
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return Tuple.Create(flag, Ret);
}
/// <summary>允许给POST请求的presigned URL设置策略,比如接收对象上传的存储桶名称的策略,key名称前缀,过期策略。
/// 允许给POST请求的presigned URL设置策略,比如接收对象上传的存储桶名称的策略,key名称前缀,过期策略。
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="PostPolicy">对象的post策略</param>
/// <returns></returns>
public async static Task<Tuple<bool, string, Dictionary<string, string>>> PresignedPostPolicy(MinioClient minio)
{
bool flag = false;
Tuple<string, Dictionary<string, string>> tdic = null;
try
{
PostPolicy form = new PostPolicy();
DateTime expiration = DateTime.UtcNow;
form.SetExpires(expiration.AddDays(10));
form.SetKey("my-objectname");
form.SetBucket("my-bucketname");
Tuple<string, Dictionary<string, string>> tuple = await minio.PresignedPostPolicyAsync(form);
tdic = tuple;
flag = true;
//string curlCommand = "curl -X POST ";
//foreach (KeyValuePair<string, string> pair in tuple.Item2)
//{
// curlCommand = curlCommand + $" -F {pair.Key}={pair.Value}";
//}
//curlCommand = curlCommand + " -F file=@/etc/bashrc " + tuple.Item1; // https://s3.amazonaws.com/my-bucketname";
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return Tuple.Create(flag, tdic.Item1, tdic.Item2);
}
#endregion
#region 操作对象
/// <summary>返回对象数据的流
/// 返回对象数据的流
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="objectName">存储桶里的对象名称</param>
/// <param name="callback">处理流的回调函数</param>
/// <returns></returns>
public async static Task<bool> GetObjectAsync(MinioClient minio, string bucketName, string objectName, Action<Stream> callback)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
await minio.StatObjectAsync(bucketName, objectName);
await minio.GetObjectAsync(bucketName, objectName, callback);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (MinioException e)
{
throw new Exception(e.Message);
}
return flag;
}
/// <summary>下载对象指定区域的字节数组做为流。offset和length都必须传
/// 下载对象指定区域的字节数组做为流。offset和length都必须传
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="objectName">存储桶里的对象名称</param>
/// <param name="offset">offset 是起始字节的位置</param>
/// <param name="length">length是要读取的长度</param>
/// <param name="callback">处理流的回调函数</param>
/// <returns></returns>
public async static Task<bool> GetObjectAsync(MinioClient minio, string bucketName, string objectName, long offset, long length, Action<Stream> callback)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
await minio.StatObjectAsync(bucketName, objectName);
await minio.GetObjectAsync(bucketName, objectName, offset, length, callback);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (MinioException e)
{
throw new Exception(e.Message);
}
return flag;
}
/// <summary>下载并将文件保存到本地文件系统
/// 下载并将文件保存到本地文件系统
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="objectName">存储桶里的对象名称</param>
/// <param name="fileName">本地文件路径</param>
/// <returns></returns>
public async static Task<bool> GetObjectAsync(MinioClient minio, string bucketName, string objectName, string fileName)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
if (File.Exists(fileName))
{
File.Delete(fileName);
}
await minio.StatObjectAsync(bucketName, objectName);
await minio.GetObjectAsync(bucketName, objectName, fileName);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (MinioException e)
{
throw new Exception(e.Message);
}
return flag;
}
/// <summary>通过文件上传到对象中
/// 通过文件上传到对象中
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="objectName">存储桶里的对象名称</param>
/// <param name="filePath">要上传的本地文件名</param>
/// <param name="contentType">文件的Content type,默认是"application/octet-stream"</param>
/// <param name="metaData">元数据头信息的Dictionary对象,默认是null</param>
/// <returns></returns>
public async static Task<bool> PutObjectAsync(MinioClient minio, string bucketName, string objectName, string filePath, string contentType = "application/octet-stream", Dictionary<string, string> metaData = null)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
await minio.PutObjectAsync(bucketName, objectName, filePath, contentType, metaData);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return flag;
}
/// <summary>通过Stream上传对象
/// 通过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<bool> PutObjectAsync(MinioClient minio, string bucketName, string objectName, Stream data, long size, string contentType = "application/octet-stream", Dictionary<string, string> metaData = null)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
//byte[] bs = File.ReadAllBytes(fileName);
//System.IO.MemoryStream filestream = new System.IO.MemoryStream(bs);
await minio.PutObjectAsync(bucketName, objectName, data, size, contentType, metaData);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (MinioException e)
{
throw new Exception(e.Message);
}
return flag;
}
/// <summary>获取对象的元数据
/// 获取对象的元数据
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="objectName">存储桶里的对象名称</param>
/// <returns></returns>
public async static Task<bool> StatObject(MinioClient minio, string bucketName, string bucketObject)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
ObjectStat statObject = await minio.StatObjectAsync(bucketName, bucketObject);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (MinioException e)
{
throw new Exception(e.Message);
}
return flag;
}
/// <summary>从objectName指定的对象中将数据拷贝到destObjectName指定的对象
/// 从objectName指定的对象中将数据拷贝到destObjectName指定的对象
/// </summary>
/// <param name="minio"></param>
/// <param name="fromBucketName">源存储桶名称</param>
/// <param name="fromObjectName">源存储桶中的源对象名称</param>
/// <param name="destBucketName">目标存储桶名称</param>
/// <param name="destObjectName">要创建的目标对象名称,如果为空,默认为源对象名称</param>
/// <param name="copyConditions">拷贝操作的一些条件Map</param>
/// <param name="sseSrc"></param>
/// <param name="sseDest"></param>
/// <returns></returns>
public async static Task<bool> CopyObject(MinioClient minio, string fromBucketName, string fromObjectName, string destBucketName, string destObjectName, CopyConditions copyConditions = null, ServerSideEncryption sseSrc = null, ServerSideEncryption sseDest = null)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(fromBucketName);
if (!found)
{
throw new Exception(string.Format("源存储桶[{0}]不存在", fromBucketName));
}
bool foundtwo = await minio.BucketExistsAsync(destBucketName);
if (!foundtwo)
{
throw new Exception(string.Format("目标存储桶[{0}]不存在", destBucketName));
}
await minio.CopyObjectAsync(fromBucketName, fromObjectName, destBucketName, destObjectName, copyConditions, null, sseSrc, sseDest);
flag = true;
}
catch (MinioException e)
{
throw new Exception(e.Message);
}
return flag;
}
/// <summary>删除一个对象
/// 删除一个对象
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="objectName">存储桶里的对象名称</param>
/// <returns></returns>
public async static Task<bool> RemoveObject(MinioClient minio, string bucketName, string objectName)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
await minio.RemoveObjectAsync(bucketName, objectName);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (MinioException e)
{
throw new Exception(e.Message);
}
return flag;
}
/// <summary>删除多个对象
/// 删除多个对象
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="objectsList">含有多个对象名称的IEnumerable</param>
/// <returns></returns>
public static async Task<bool> RemoveObjects(MinioClient minio, string bucketName, List<string> objectsList)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
if (objectsList != null)
{
IObservable<DeleteError> objectsOservable = await minio.RemoveObjectAsync(bucketName, objectsList).ConfigureAwait(false);
flag = true;
//IDisposable objectsSubscription = objectsOservable.Subscribe(
// objDeleteError => Console.WriteLine($"Object: {objDeleteError.Key}"),
// ex => Console.WriteLine($"OnError: {ex}"),
// () =>
// {
// Console.WriteLine($"Removed objects in list from {bucketName}\n");
// });
//return;
}
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (MinioException e)
{
throw new Exception(e.Message);
}
return flag;
}
/// <summary>删除一个未完整上传的对象
/// 删除一个未完整上传的对象
/// </summary>
/// <param name="minio">连接实例</param>
/// <param name="bucketName">存储桶名称</param>
/// <param name="objectName">存储桶里的对象名称</param>
/// <returns></returns>
public async static Task<bool> RemoveIncompleteUpload(MinioClient minio, string bucketName, string objectName)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
await minio.RemoveIncompleteUploadAsync(bucketName, objectName);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (MinioException e)
{
throw new Exception(e.Message);
}
return flag;
}
#endregion
}
到了这里,关于windows系统部署minio,以及添加到系统服务器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!