Browse Source

添加Oss的Signature签名

DuGuYang 1 year ago
parent
commit
1c6a9118c7
1 changed files with 24 additions and 4 deletions
  1. 24 4
      Areas/Api/Controllers/v1/MainServer/ConsumersController.cs

+ 24 - 4
Areas/Api/Controllers/v1/MainServer/ConsumersController.cs

@@ -12,6 +12,7 @@ using LitJson;
 using Library;
 using System.Security.Cryptography;
 using System.Text;
+using Aliyun.OSS;
 
 namespace MySystem.Areas.Api.Controllers.v1
 {
@@ -61,7 +62,7 @@ namespace MySystem.Areas.Api.Controllers.v1
                 HeadPhoto = consumer.HeadPhoto;
                 NickName = consumer.NickName;
             }
-            var token = PublicFunction.AppToken(consumer.Id,JwtSecret,JwtIss);
+            var token = PublicFunction.AppToken(consumer.Id, JwtSecret, JwtIss);
             Obj.Add("Amount", TotalAmount); //累计返现金额
             Obj.Add("HeadPhoto", HeadPhoto); //头像
             Obj.Add("NickName", NickName); //昵称
@@ -78,9 +79,28 @@ namespace MySystem.Areas.Api.Controllers.v1
         public JsonResult OssInfo()
         {
             Dictionary<string, object> Obj = new Dictionary<string, object>();
-            Obj.Add("OssUrl", "laikeba.oss-cn-chengdu.aliyuncs.com");
-            Obj.Add("AccessKeyId", "LTA15tJsPaNzgCSMCPwb8zfz");
-            Obj.Add("AccessKeySecret", "efM31Up75f0cgZ32U6xvAciagceQae");
+            var OssUrl = "laikeba.oss-cn-chengdu.aliyuncs.com";
+            var AccessKeyId = "LTA15tJsPaNzgCSMCPwb8zfz";
+            var AccessKeySecret = "efM31Up75f0cgZ32U6xvAciagceQae";
+
+            var dir = DateTime.Now.ToString("yyyyMMdd") + "/";
+            // 构造OssClient实例。 endpoint 格式:https://oss-cn-beijing.aliyuncs.com
+            var ossClient = new OssClient("https://" + OssUrl, 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);
+            Obj.Add("OssUrl", OssUrl);
+            Obj.Add("AccessKeyId", AccessKeyId);
+            Obj.Add("AccessKeySecret", AccessKeySecret);
+            Obj.Add("Signature", Signature);
             return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
         }
         #endregion