using Attribute; using Dto; using Vo; using Model; using Service; using System.Text; using System.Security.Cryptography; using Aliyun.OSS; using Infrastructure; namespace Services.Base { /// /// app底部导航Service业务层处理 /// [AppService(ServiceType = typeof(IOssService), ServiceLifetime = LifeTime.Transient)] public class OssService : IOssService { /// /// 前端上传oss返回参数 /// /// 请求参数 /// 前端上传oss返回参数 public OssUploadVo uploadInfo(OssUploadDto param) { string dir = param.dir; //文件上传路径 var options = App.OptionsSetting; var oss = options.ossConfigs; var OssUrl = oss.Host; var AccessKeyId = oss.Key; var AccessKeySecret = oss.Secret; var endpoint = "https://" + oss.Endpoint; // 构造OssClient实例。 endpoint 格式:https://oss-cn-beijing.aliyuncs.com var ossClient = new OssClient(endpoint, AccessKeyId, AccessKeySecret); var config = new PolicyConditions(); config.AddConditionItem(PolicyConditions.CondContentLengthRange, 1, 1024L * 1024 * 1024 * 5);// 文件大小范围:单位byte config.AddConditionItem(MatchMode.StartWith, PolicyConditions.CondKey, dir); var expire = DateTimeOffset.Now.AddMinutes(30);// 过期时间 // 生成 Policy,并进行 Base64 编码 var policy = ossClient.GeneratePostPolicy(expire.LocalDateTime, config); var policyBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(policy)); // 计算签名 var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(AccessKeySecret)); var bytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(policyBase64)); var Signature = Convert.ToBase64String(bytes); var response = new OssUploadVo() { ossUrl = OssUrl, accessKeyId = AccessKeyId, policy = policyBase64, expiration = expire, signature = Signature, dir = dir, }; return response; } } }