using Aliyun.OSS; using System; namespace WX.CRM.Common { public class AliyunHelper { #region 阿里云图片上传 /// /// 设置上传的文件的key值 /// /// /// public static string UploadImage(string path, string imgKey) { #region //iLCVNfsiH4F1JLNU //24DOemjGGeQeG2WJNzHBqi1wNtySgB //http://qp-wechat-img.oss-cn-shenzhen.aliyuncs.com //qp-wechat-img string aliAccessKeyId = Utility.GetSettingByKey("AliAccessKeyId") ?? "iLCVNfsiH4F1JLNU"; string aliAccessKeySecret = Utility.GetSettingByKey("AliAccessKeySecret") ?? "24DOemjGGeQeG2WJNzHBqi1wNtySgB"; string aliBucketHost = Utility.GetSettingByKey("AliBucketHost") ?? "http://qp-wechat-img.oss-cn-shenzhen.aliyuncs.com"; string aliBucket = Utility.GetSettingByKey("AliBucket") ?? "qp-wechat-img"; #endregion if (string.IsNullOrEmpty(aliAccessKeyId) || string.IsNullOrEmpty(aliAccessKeySecret) || string.IsNullOrEmpty(aliBucketHost) || string.IsNullOrEmpty(aliBucket)) { throw new Exception("阿里oss配置错误"); } var ossClient = new OssClient(aliBucketHost, aliAccessKeyId, aliAccessKeySecret); try { var result = ossClient.PutObject(aliBucket, imgKey, path); return aliBucketHost + "/" + imgKey; } catch (Exception es) { LogHelper.Error(string.Format("Put object failed, {0}", es.Message)); } return null; } #endregion public static string UploadImage(string path) { if (string.IsNullOrWhiteSpace(path)) { throw new Exception("路径不能为空!"); } try { var md5 = Utility.GetMD5HashFromFile(path); return UploadImage(path, md5); } catch { throw; } } } }