/// <summary> /// 文件上传 /// </summary> /// <param name="file"></param> /// <param name="fileName"></param> /// <returns></returns> public string FileSaveAliyunOSS(HttpPostedFileBase file, string fileName) { 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);//扩展名 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 = "application/octet-stream", }; //文件上传--空间名、文件保存路径、文件流、meta头信息(文件md5) //返回meta头信息(文件md5) PutObjectResult por = aliyun.PutObject(BucketName, FilePath, fileStream, objectMeta); string text = por.ETag; if (extension.ToUpper() == ".JPG") { FilePath += "-big"; } } 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/9523183.html
时间: 2024-10-31 19:40:12