阿裏雲圖片保存
阿新 • • 發佈:2018-08-23
ssa code 擴展 type 上傳 put object for cat
/// <summary> /// 阿裏雲圖片保存 /// </summary> /// <param name="file"></param> /// <param name="fileName"></param> /// <param name="op">0代表是默認的圖片上傳,1代表是新聞的圖片上傳</param> /// <returns></returns> private string SaveAliyunOSS(HttpPostedFileBase file, stringfileName, int op=0) { string FilePath = ""; //上傳到阿裏雲 using (Stream fileStream = file.InputStream)//轉成Stream流 { string md5 = OssUtils.ComputeContentMd5(fileStream, file.ContentLength); string today = DateTime.Now.ToString("yyyyMMdd"); string extension = System.IO.Path.GetExtension(file.FileName);//擴展名 if (op==1) { extension = ".jpeg"; } string FileName = Convert.ToString(Guid.NewGuid()).Replace("-", "") + extension;//文件名=文件名+當前上傳時間FilePath = fileName + "/" + Convert.ToString(DateTime.Now.Year) + "/" + Convert.ToString(DateTime.Now.Month) + "/" + FileName;//雲文件保存路徑 try { //初始化阿裏雲配置--外網Endpoint、訪問ID、訪問password OssClient aliyun = new OssClient(endpoint, accessKeyId, accessKeySecret); //將文件md5值賦值給meat頭信息,服務器驗證文件MD5 var objectMeta = new ObjectMetadata { ContentMd5 = md5, ContentType = "image/jpeg", }; //文件上傳--空間名、文件保存路徑、文件流、meta頭信息(文件md5) //返回meta頭信息(文件md5) PutObjectResult por = aliyun.PutObject(BucketName, FilePath, fileStream, objectMeta); string text = por.ETag; } catch (Exception e) { ICE.Core.LogHelper.ErrorLog(log, string.Format("圖片上傳OSS出錯了:{0}", Convert.ToString(e.Message))); FilePath = ""; } } return FilePath; }
阿裏雲圖片保存