要安装包nito
using Nito.AsyncEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace SLFQ.Util.Minio
{
public static class AsyncHelper
{
/// <summary>
/// Checks if given method is an async method.
/// </summary>
/// <param name="method">A method to check</param>
public static bool IsAsync(this MethodInfo method)
{
return (
method.ReturnType == typeof(Task) ||
(method.ReturnType.GetTypeInfo().IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>))
);
}
/// <summary>
/// Checks if given method is an async method.
/// </summary>
/// <param name="method">A method to check</param>
[Obsolete("Use MethodInfo.IsAsync() extension method!")]
public static bool IsAsyncMethod(MethodInfo method)
{
return method.IsAsync();
}
/// <summary>
/// Runs a async method synchronously.
/// </summary>
/// <param name="func">A function that returns a result</param>
/// <typeparam name="TResult">Result type</typeparam>
/// <returns>Result of the async operation</returns>
public static TResult RunSync<TResult>(Func<Task<TResult>> func)
{
return AsyncContext.Run(func);
}
/// <summary>
/// Runs a async method synchronously.
/// </summary>
/// <param name="action">An async action</param>
public static void RunSync(Func<Task> action)
{
AsyncContext.Run(action);
}
}
}
Minio方法
using Minio;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace SLFQ.Util.Minio
{
public class MinioHelper
{
private static string endPoint = "你的";
private static string accessKey = "XXXX";
private static string secretKey = "XXXX";
private static string BucketName = "XXXX";
private static bool UseSsl = true;
/// <summary>
///
/// </summary>
/// <returns></returns>
public static MinioClient GetMinioClient()
{
endPoint = endPoint.Replace("http://", "").Replace("https://", "");
if (UseSsl)
{
return new MinioClient(endPoint, accessKey, secretKey).WithSSL();
}
return new MinioClient(endPoint, accessKey, secretKey);
}
/// <summary>
///
/// </summary>
/// <param name="minio"></param>
/// <param name="filePath"></param>
/// <param name="objectName"></param>
/// <returns></returns>
public async static Task Upload(MinioClient minio, string filePath, string objectName)
{
var found = await minio.BucketExistsAsync(BucketName);
if (!found)
{
await minio.MakeBucketAsync(BucketName);
}
await minio.PutObjectAsync(BucketName, objectName, filePath);
}
/// <summary>
///
/// </summary>
/// <param name="minio"></param>
/// <param name="stream"></param>
/// <param name="objectName"></param>
/// <returns></returns>
public async static Task Upload(MinioClient minio, Stream stream, string objectName)
{
var found = await minio.BucketExistsAsync(BucketName);
if (!found)
{
await minio.MakeBucketAsync(BucketName);
}
await minio.PutObjectAsync(BucketName, objectName, stream, stream.Length);
}
/// <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 (Exception e)
{
throw e;
}
return flag;
}
/// <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, string fileName)
{
bool flag = false;
try
{
bool found = await minio.BucketExistsAsync(bucketName);
if (found)
{
await minio.StatObjectAsync(bucketName, objectName);
await minio.GetObjectAsync(bucketName, objectName, fileName);
flag = true;
}
else
{
throw new Exception(string.Format("存储桶[{0}]不存在", bucketName));
}
}
catch (Exception e)
{
throw e;
}
return flag;
}
}
}
上传页面
var minio = MinioHelper.GetMinioClient();
try
{
AsyncHelper.RunSync(() => MinioHelper.Upload(minio, fullFileName, fileName));
//return Success("上传成功。");
}
catch (Exception ex)
{
//Console.WriteLine(ex.Message);
return Fail(ex.Message);
}