/// <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>
 /// 获得OSS里面文件的网络地址
 /// </summary>
 /// <param name="key">Key</param>
 /// <param name="bucketName">存储空间(Bucket)名称</param>
 /// <param name="bucketType">存储空间(Bucket)类型</param>
 /// <returns>返回网络地址</returns>
 /// <Author>旷丽文</Author>
 public string GetOssFileUrl(string key, string bucketName, BucketType bucketType)
 {
     if (key.IsNullOrWhiteSpace())
     {
         throw new Exception("Key不能为空");
     }
     key        = key.Trim();
     bucketName = $"{AppSettings["OSSBucketName"]}{bucketType.ToString()}{bucketName}".ToLower();
     return($"http://{bucketName}.{this.OSSAddress}/{key}");
 }
Exemple #5
0
 //one of the many ways to describe a sale
 public void PrintSale()
 {
     if (BucketType != BucketTypes.Single)
     {
         Console.WriteLine(date.ToString() + " : Bucket : " + BucketType.ToString() + " cost : " + cost.ToString());
     }
     else
     {
         Console.WriteLine(date.ToString() + " : Flower : " + FlowerType.ToString() + " cost : " + cost.ToString());
     }
 }
 /// <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);
     }
 }