Ver código fonte

增加前端上传oss返回参数接口

lcl 3 meses atrás
pai
commit
9049d8e9e2
1 arquivos alterados com 55 adições e 0 exclusões
  1. 55 0
      Areas/Api/Controllers/OutApiController.cs

+ 55 - 0
Areas/Api/Controllers/OutApiController.cs

@@ -9,6 +9,9 @@ using System.Linq;
 using System;
 using MySystem.MainModels;
 using System.IO;
+using System.Text;
+using Aliyun.OSS;
+using System.Security.Cryptography;
 
 namespace MySystem.Areas.Api.Controllers.v1
 {
@@ -177,6 +180,58 @@ namespace MySystem.Areas.Api.Controllers.v1
         #endregion
 
 
+
+
+
+
+        #region 前端上传oss返回参数
+        [Route("/api/oss/uploadinfo")]
+        public JsonResult OssInfo(string value)
+        {
+            value = DesDecrypt(value);
+            JsonData data = JsonMapper.ToObject(value);
+            string dir = data["dir"].ToString(); //文件上传路径        
+            if (string.IsNullOrEmpty(data["dir"].ToString()))
+            {
+                return Json(new AppResultJson() { Status = "1", Info = "文件上传路径不能为空" });
+            }
+            Dictionary<string, object> Obj = new Dictionary<string, object>();
+            var OssUrl = "https://laikeba.oss-cn-chengdu.aliyuncs.com";
+            var AccessKeyId = "LTAI5tJsPaNzqCSMCPwb8zfz";
+            var AccessKeySecret = "efM31Up75fQcgZ32U6xvAciagceQae";
+            var endpoint = "https://oss-cn-chengdu.aliyuncs.com";
+            // "OSSKey": "LTAI5tJsPaNzqCSMCPwb8zfz",
+            // "OSSSecret": "efM31Up75fQcgZ32U6xvAciagceQae",
+            // "OSSEndpoint": "oss-cn-chengdu.aliyuncs.com",
+            // "OSSBucketName": "laikeba",
+
+            // 构造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);
+
+            Obj.Add("OssUrl", OssUrl);
+            Obj.Add("AccessKeyId", AccessKeyId);
+            // Obj.Add("AccessKeySecret", AccessKeySecret);
+            Obj.Add("Policy", policyBase64);
+            Obj.Add("Expiration", expire);
+            Obj.Add("Signature", Signature);
+            Obj.Add("dir", dir);
+            return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
+        }
+        #endregion
+
         // [Route("/api/v1/{c1}/{c2}")]
         // public JsonResult ToApServer(string c1, string c2, string value)
         // {