MinIO的官方网站非常详细,以下只是本人学习过程的整理

一、MinIO的基本概念

二、Windows安装与简单使用MinIO

三、Linux部署MinIO分布式集群

四、C#简单操作MinIO

Healper类

using Minio;
using Minio.DataModel;
using Minio.Exceptions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace TestMinIO
{
public static class MinIO
{
#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
}
}

枚举类

using System;
using System.Collections.Generic;
using System.Text; namespace TestMinIO
{
/// <summary>ContentType枚举类
/// ContentType枚举类
/// </summary>
public enum Content_Type
{
bmp,
biz,
exe,
gif,
html,
ico,
img,
jpe,
jpeg,
jpg,
movie,
mp3,
mp4,
pdf,
png,
ppt,
txt,
xls,
xsl,
apk
} /// <summary>ContentType枚举帮助类
/// ContentType枚举帮助类
/// </summary>
public static class Helper
{
/// <summary>
/// 根据枚举返回ContentType
/// </summary>
/// <param name="type">枚举类型</param>
/// <returns></returns>
public static string DoContentType(Content_Type type)
{
string Result = "application/octet-stream";
switch (type)
{
case Content_Type.bmp:
Result = "application/x-bmp";
break;
case Content_Type.biz:
Result = "text/xml";
break;
case Content_Type.exe:
Result = "application/x-msdownload";
break;
case Content_Type.gif:
Result = "image/gif";
break;
case Content_Type.html:
Result = "text/html";
break;
case Content_Type.ico:
Result = "image/x-icon";
break;
case Content_Type.img:
Result = "application/x-img";
break;
case Content_Type.jpe:
Result = "image/jpeg";
break;
case Content_Type.jpeg:
Result = "image/jpeg";
break;
case Content_Type.jpg:
Result = "image/jpeg";
break;
case Content_Type.movie:
Result = "video/x-sgi-movie";
break;
case Content_Type.mp3:
Result = "audio/mp3";
break;
case Content_Type.mp4:
Result = "video/mpeg4";
break;
case Content_Type.pdf:
Result = "application/pdf";
break;
case Content_Type.png:
Result = "image/png";
break;
case Content_Type.ppt:
Result = "application/x-ppt";
break;
case Content_Type.txt:
Result = "text/plain";
break;
case Content_Type.xls:
Result = "application/x-xls";
break;
case Content_Type.xsl:
Result = "text/xml";
break;
case Content_Type.apk:
Result = "application/vnd.android.package-archive";
break;
}
return Result;
}
}
}

调用方法很简单,此处就不展示啦

转载请注明出处

四、C#简单操作MinIO的更多相关文章

  1. 二、Windows安装与简单使用MinIO

    MinIO的官方网站非常详细,以下只是本人学习过程的整理 一.MinIO的基本概念 二.Windows安装与简单使用MinIO 三.Linux部署MinIO分布式集群 四.C#简单操作MinIO 一. ...

  2. 从零开始学习jQuery (四) 使用jQuery操作元素的属性与样式

    本系列文章导航 从零开始学习jQuery (四) 使用jQuery操作元素的属性与样式 一.摘要 本篇文章讲解如何使用jQuery获取和操作元素的属性和CSS样式. 其中DOM属性和元素属性的区分值得 ...

  3. Linq对XML的简单操作

    前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...

  4. Linux 中 Vi 编辑器的简单操作

    Linux 中 Vi 编辑器的简单操作 Vi 编辑器一共有3种模式:命名模式(默认),尾行模式,编辑模式.3种模式彼此需要切换. 一.进入 Vi 编辑器的的命令 vi  filename //打开或新 ...

  5. X-Cart 学习笔记(四)常见操作

    目录 X-Cart 学习笔记(一)了解和安装X-Cart X-Cart 学习笔记(二)X-Cart框架1 X-Cart 学习笔记(三)X-Cart框架2 X-Cart 学习笔记(四)常见操作 五.常见 ...

  6. ZooKeeper系列3:ZooKeeper命令、命令行工具及简单操作

    问题导读1.ZooKeeper包含哪些常用命令?2.通过什么命令可以列出服务器 watch 的详细信息?3.ZooKeeper包含哪些操作?4.ZooKeeper如何创建zookeeper? 常用命令 ...

  7. Nginx简单操作

    Nginx简单操作 平滑重启:读取配置文件,正确后启动新nginx,关闭旧服务进程 # kill HUP nginx.pid # /usr/sbin/nginx -c /etc/nginx/nginx ...

  8. matlab矩阵的表示和简单操作

    原地址:http://www.cnblogs.com/Ran_Ran/archive/2010/12/11/1903070.html 一.矩阵的表示在MATLAB中创建矩阵有以下规则: a.矩阵元素必 ...

  9. .Net Core MongoDB 简单操作。

    一:MongoDB 简单操作类.这里引用了MongoDB.Driver. using MongoDB.Bson; using MongoDB.Driver; using System; using S ...

随机推荐

  1. Zabbix5.0微信报警

    3.1.注测企业微信: 3.2.企业微信注册成功后进入后台管理: 3.3.添加一个部门,并记住部门id: #我这里添加的子部门ID为2 3.4.添加一个用户到上面创建的部门里面(这里采取直接将管理员添 ...

  2. 24、dhcp服务搭建

    1.dhcp介绍: DHCP(Dynamic Host Configuration Protocol),动态主机配置协议,DHCP 协议主要是用来自动为局域网中的客户机分配 TCP/IP 信息的网络协 ...

  3. Cygwin-OpenSSH配置手册及常见问题解决

    右键管理员方式运行Cygwin-setup-x86_64.exe 选择Install from Local Ddirectory 选择安装路径(默认下一步) 选择依赖库路径 依次配置一下选项 接下来安 ...

  4. MPP大规模并行处理架构详解

    面试官:说下你知道的MPP架构的计算引擎? 这个问题不少小伙伴在面试时都遇到过,因为对MPP这个概念了解较少,不少人都卡壳了,但是我们常用的大数据计算引擎有很多都是MPP架构的,像我们熟悉的Impal ...

  5. Linux(CentOS)下安装docker

    Linux(CentOS)安装Docker 查看当前内核版本 [docker@localhost ~]$ uname -r 确保yum包更新到最新 [docker@localhost ~]$ sudo ...

  6. 调整/home和/root空间容量

    转载请注明出处:http://www.cnblogs.com/gaojiang/p/6767043.html 1.查看磁盘情况:df -h 2.卸载/homeumount /home umount / ...

  7. 密码学系列之:memory-bound函数

    密码学系列之:memory-bound函数 目录 简介 内存函数 内存受限函数 内存受限函数的使用 简介 memory-bound函数可以称为内存受限函数,它是指完成给定计算问题的时间主要取决于保存工 ...

  8. 获取操作系统OS等相关信息

    问题一:Windows SDK 8.1版本中的VersionHelper.h文件当中没有IsWindows10ORGreater,所以当你用IsWindows8Point1ORGreater判断出版本 ...

  9. XDFZOI 月赛 201905 Sliver

    组题人自己组完过后,才发现自己还是太弱了... T1 简单模拟. 按照游戏规则直接模拟显然是不明智的,所以我们可以像石头剪刀布一样,将判断改变为检验. 同时,我们发现,一共只有48种牌,所以我们可以直 ...

  10. Spring BeanFactory和现实工厂的对比

    本文不分析Spring的源码流程,只是介绍一些基础的概念,在阅读源码之前,我们应该首先明确研究的对象是什么,才能有的放矢. Spring作为BeanFactory, 和现实工厂有着许多类似之处. 需要 ...