/// <summary>
 /// 上传文件到OSS
 /// </summary>
 /// <param name="file">文件内存流</param>
 /// <param name="key">Key</param>
 /// <param name="bucketName">存储空间(Bucket)名称</param>
 /// <param name="bucketType">存储空间(Bucket)类型</param>
 /// <returns></returns>
 /// <Author>旷丽文</Author>
 public PutObjectResult PutFile(Stream file, string key, string bucketName, BucketType bucketType)
 {
     if (file == null)
     {
         throw new Exception("文件为空");
     }
     if (key.IsNullOrWhiteSpace())
     {
         throw new Exception("Key不能为空");
     }
     if (bucketName.IsNullOrWhiteSpace())
     {
         throw new Exception("bucket不能为空");
     }
     try
     {
         key        = key.Trim();
         bucketName = $"{AppSettings["OSSBucketName"]}{bucketType.ToString()}{bucketName}".ToLower();
         var result = this.Client.PutObject(bucketName, key, file);
         return(result);
     }
     catch (Exception ex)
     {
         LogerManager.Error(ex);
         return(null);
     }
     finally
     {
         file.Close();
         file.Dispose();
     }
 }
 /// <summary>
 /// 上传字符串到OSS
 /// </summary>
 /// <param name="value">字符串值</param>
 /// <param name="key">Key</param>
 /// <param name="bucketName">存储空间(Bucket)名称</param>
 /// <param name="bucketType">存储空间(Bucket)类型</param>
 /// <returns></returns>
 /// <Author>旷丽文</Author>
 public PutObjectResult PutString(string value, string key, string bucketName, BucketType bucketType)
 {
     if (value.IsNullOrWhiteSpace())
     {
         throw new Exception("字符串不能为空");
     }
     if (key.IsNullOrWhiteSpace())
     {
         throw new Exception("Key不能为空");
     }
     if (bucketName.IsNullOrWhiteSpace())
     {
         throw new Exception("bucket不能为空");
     }
     value = value.Trim();
     try
     {
         key        = key.Trim();
         bucketName = $"{AppSettings["OSSBucketName"]}{bucketType.ToString()}{bucketName}".ToLower();
         byte[]       binaryData     = Encoding.ASCII.GetBytes(value);
         MemoryStream requestContent = new MemoryStream(binaryData);
         var          result         = this.Client.PutObject(bucketName, key, requestContent);
         requestContent.Close();
         requestContent.Dispose();
         return(result);
     }
     catch (Exception ex)
     {
         LogerManager.Error(ex);
         return(null);
     }
 }
 /// <summary>
 /// 创建一个新的存储空间(Bucket)
 /// 不要平凡的创建存储空间
 /// </summary>
 /// <param name="bucketName">存储控件的名称,该名称一定要唯一</param>
 /// <param name="bucketType">存储空间类型</param>
 /// <returns>创建成功的话, 返回Bucket对象, 否则返回null</returns>
 public Bucket CreateBucket(string bucketName, BucketType bucketType)
 {
     if (bucketName.IsNullOrWhiteSpace())
     {
         throw new Exception("bucket不能为空");
     }
     try
     {
         var exist = this.BucketExist(bucketName, bucketType);
         if (exist == null)
         {
             throw new Exception("创建失败");
         }
         if ((bool)exist)
         {
             throw new Exception("该Bucket已经存在, 请更换名称");
         }
         bucketName = $"{AppSettings["OSSBucketName"]}{bucketType.ToString()}{bucketName}".ToLower();
         return(this.Client.CreateBucket(bucketName));
     }
     catch (Exception ex)
     {
         LogerManager.Error(ex);
         return(null);
     }
 }
 /// <summary>
 /// 获得所有的存储空间(Bucket)
 /// </summary>
 /// <returns>失败返回null</returns>
 /// <Author>旷丽文</Author>
 public IEnumerable <Bucket> GetListBuckets()
 {
     try
     {
         return(this.Client.ListBuckets());
     }
     catch (Exception ex)
     {
         LogerManager.Error(ex);
         return(null);
     }
 }
 /// <summary>
 /// 设置存储空间的访问权限
 /// </summary>
 /// <param name="buckteName">存储空间的名称</param>
 /// <param name="bucketType">存储空间(Bucket)类型</param>
 public void SetBucketAcl(string buckteName, BucketType bucketType)
 {
     try
     {
         buckteName = $"{AppSettings["OSSBucketName"]}{bucketType.ToString()}{buckteName}".ToLower();
         // 指定Bucket ACL为公共读
         this.Client.SetBucketAcl(buckteName, CannedAccessControlList.PublicRead);
     }
     catch (Exception ex)
     {
         LogerManager.Error(ex);
     }
 }
 /// <summary>
 /// 判断存储空间(Bucket)是否存在
 /// </summary>
 /// <param name="bucketName"></param>
 /// <param name="bucketType"></param>
 /// <returns>是否存在的bool, 调用失败返回null</returns>
 /// <Author>旷丽文</Author>
 public bool?BucketExist(string bucketName, BucketType bucketType)
 {
     if (bucketName.IsNullOrWhiteSpace())
     {
         throw new Exception("bucket不能为空");
     }
     try
     {
         bucketName = $"{AppSettings["OSSBucketName"]}{bucketType.ToString()}{bucketName}".ToLower();
         return(this.Client.DoesBucketExist(bucketName));
     }
     catch (Exception ex)
     {
         LogerManager.Error(ex);
         return(null);
     }
 }
 /// <summary>
 /// 下载文件
 /// </summary>
 /// <param name="key">Key</param>
 /// <param name="bucketName">存储空间(Bucket)名称</param>
 /// <param name="bucketType">存储空间(Bucket)类型</param>
 /// <returns></returns>
 /// <Author>旷丽文</Author>
 public Stream DownFile(string key, string bucketName, BucketType bucketType)
 {
     if (key.IsNullOrWhiteSpace())
     {
         throw new Exception("Key不能为空");
     }
     try
     {
         key        = key.Trim();
         bucketName = $"{AppSettings["OSSBucketName"]}{bucketType.ToString()}{bucketName}".ToLower();
         var file = this.Client.GetObject(bucketName, key);
         return(file.Content);
     }
     catch (Exception ex)
     {
         LogerManager.Error(ex);
         return(null);
     }
 }
Esempio n. 8
0
        /// <summary>
        /// 执行方法, 并且返回
        /// </summary>
        /// <typeparam name="Result">返回值类型, 如果执行失败, 返回的是default(Result)的值</typeparam>
        /// <param name="func">委托</param>
        /// <returns>BusinesResult</returns>

        protected BusinessResult <Result> Runing <Result>(Func <Result> func)
        {
            try
            {
                if (func == null)
                {
                    throw new Exception("方法体为空, 处理无意义, 异常来自BusinesBase类的Runing方法");
                }
                Result result = func();
                return(new BusinessResult <Result>
                {
                    Msg = string.Empty,
                    Success = true,
                    Value = result
                });
            }
            catch (ParamsException ex)
            {
                return(new BusinessResult <Result>
                {
                    Msg = ex.Message,
                    Success = false,
                    Value = default(Result),
                    Exception = ex
                });
            }
            catch (Exception ex)
            {
                LogerManager.Error(ex);
                return(new BusinessResult <Result>
                {
                    Msg = ex.Message,
                    Success = false,
                    Value = default(Result),
                    Exception = ex
                });
            }
        }