登录 / 注册
.netFramework4.6.1与Minio图片上传
发布时间:2022-09-15 17:36:11 查看:2707

要安装包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);
                }

宁夏银川舜新艺软件开发
ShunXinyi Soft develop
主要经营
软件开发,网站制作,网页设计,移动应用(安卓、苹果),微信,微网站,
FLASH动画,电子商务,计算机软硬件及网络设备等。
电话:18695132945 QQ:23923027
舜新艺软件开发 宁ICP备16001093号-1 宁公网安备 64010602000809号