/// <summary> /// 阿里云图片保存 /// </summary> /// <param name="file"></param> /// <param name="fileName"></param> /// <param name="op">0代表是默认的图片上传,1代表是新闻的图片上传</param> /// <returns></returns> private string SaveAliyunOSS(HttpPostedFileBase file, string fileName, 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; }
原文地址:https://www.cnblogs.com/fyssl/p/9523186.html
时间: 2024-11-09 03:49:28