浏览代码

处理Blob容器授权。

CrazyIter 4 年之前
父节点
当前提交
ce9ab71aa0

+ 32 - 5
TEAMModelOS.SDK/Module/AzureBlob/Implements/AzureBlobDBRepository.cs

@@ -607,7 +607,7 @@ namespace TEAMModelOS.SDK.Module.AzureBlob.Implements
             // Return the URI string for the container, including the SAS token.
             return (blobContainer.Uri.ToString(), sasContainerToken);
         }
-        public async Task<KeyValuePair<string, object>> GetContainerSasUri(BlobSas blobSas, bool isRead)
+        public async Task<dynamic> GetContainerSasUri(BlobSas blobSas, bool isRead)
         {
             CloudBlobContainer blobContainer;
             if (blobSas.role == "system")
@@ -624,7 +624,6 @@ namespace TEAMModelOS.SDK.Module.AzureBlob.Implements
             // to construct a shared access policy that is saved to the container's shared access policies.
             DateTimeOffset dateTime = DateTime.UtcNow.AddHours(1);
             long time = dateTime.ToUnixTimeMilliseconds();
-            SharedAccessBlobPolicy adHocSAS = null;
             SharedAccessBlobPolicy adHocPolicy = null;
             if (isRead)
             {
@@ -649,8 +648,8 @@ namespace TEAMModelOS.SDK.Module.AzureBlob.Implements
             }
             // Generate the shared access signature on the container, setting the constraints directly on the signature.
             string   sasContainerToken = blobContainer.GetSharedAccessSignature(adHocPolicy, null);
-            KeyValuePair<string, object> dict = new KeyValuePair<string, object>(   blobContainer.Name,  new { url = blobContainer.Uri, sas = sasContainerToken, timeout = time } );
-            return dict;
+           
+            return new { url = blobContainer.Uri, sas = sasContainerToken, timeout = time };
         }
         public async Task<Dictionary<string,object>> GetBlobSasUri(BlobSas blobSas,bool isRead) {
             string sasBlobToken;
@@ -823,7 +822,24 @@ namespace TEAMModelOS.SDK.Module.AzureBlob.Implements
             }
             return blob.Uri + sasBlobToken;
         }
+        public async Task<dynamic> GetBlobSasUriRead(string containerName, string blobName)
+        {
+            string sasBlobToken;
+            blobContainer = await GetContainer(containerName);
+            CloudBlockBlob blob = blobContainer.GetBlockBlobReference(blobName);
+            DateTimeOffset dateTime = DateTime.UtcNow.AddHours(1);
+            long time = dateTime.ToUnixTimeMilliseconds();
+            SharedAccessBlobPolicy adHocSAS = new SharedAccessBlobPolicy()
+            {
+                SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15),
+                SharedAccessExpiryTime = dateTime,
+                Permissions = SharedAccessBlobPermissions.Read
+            };
+            sasBlobToken = blob.GetSharedAccessSignature(adHocSAS);
+            return new { url = blob.Uri, sas = sasBlobToken, timeout = time };
+        }
 
+        
 
         private async Task<CloudBlobContainer> CreateContainer(string containerName)
         {
@@ -833,7 +849,18 @@ namespace TEAMModelOS.SDK.Module.AzureBlob.Implements
             await container.CreateIfNotExistsAsync();
             return container;
         }
-
+        private async Task<CloudBlobContainer> GetContainer(string containerName)
+        {
+            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_options.ConnectionString);
+            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
+            CloudBlobContainer container = blobClient.GetContainerReference(containerName);
+            bool a = await container.ExistsAsync();
+            if (!a) {
+                throw new BizException("容器不存在!",ResponseCode.PARAMS_ERROR);
+            }
+           // await container.CreateIfNotExistsAsync();
+            return container;
+        }
 
         public async Task Deleteblob(string azureBlobSAS)
         {

+ 2 - 1
TEAMModelOS.SDK/Module/AzureBlob/Interfaces/IAzureBlobDBRepository.cs

@@ -20,6 +20,7 @@ namespace TEAMModelOS.SDK.Module.AzureBlob.Interfaces
         AzureBlobModel UploadFileByFolderNAsyn(Stream fileSteam, string folder, string fileName, string fileSpace = "pptx", bool contentTypeDefault = true);
         Task<string> GetBlobSasUri(string blobName, string containerName=null,  string policyName = null);
         Task<string> GetBlobSasUriRead(string containerName, string blobName, string policyName = null);
+        Task<dynamic> GetBlobSasUriRead(string containerName, string blobName);
        Task<(string, string, string)> GetContainerSasUri(string containerName=null, string policyName = null);
         Task<(string, string)> GetContainerSasUriRead(string containerName, string policyName = null);
         Task<bool> CreateSharedAccessPolicyAsync(string policyName , string  containerName = null);
@@ -29,7 +30,7 @@ namespace TEAMModelOS.SDK.Module.AzureBlob.Interfaces
          Task<List<BlobFileDto>> GetBlobDirectory(string containerName, string blobName);
 
         Task<Dictionary<string, object>> GetBlobSasUri(BlobSas blobSas,bool isRead);
-        Task<KeyValuePair<string, object>> GetContainerSasUri(BlobSas blobSas, bool isRead);
+        Task<dynamic> GetContainerSasUri(BlobSas blobSas, bool isRead);
 
     }
 }

+ 5 - 0
TEAMModelOS.Service/TEAMModelOS.Model.xml

@@ -541,6 +541,11 @@
              学校编码,个人编码,考试编码
             </summary>
         </member>
+        <member name="P:TEAMModelOS.Service.Models.Paper.url">
+            <summary>
+            试卷的基本信息
+            </summary>
+        </member>
         <member name="P:TEAMModelOS.Service.Models.Paper.typeScore">
             <summary>
             type:{

+ 10 - 11
TEAMModelOS/Controllers/Core/BlobController.cs

@@ -34,10 +34,9 @@ namespace TEAMModelOS.Controllers.Core
         {
             ///返回金钥过期时间
             JsonRPCResponseBuilder builder = new JsonRPCResponseBuilder();
-            Dictionary<string, object> dict = await azureBlobDBRepository.GetBlobSasUri(request.@params,true);
-            KeyValuePair<string, object> d = await azureBlobDBRepository.GetContainerSasUri(request.@params, true);
-            dict.Add(d.Key, d.Value);
-            return builder.Data(dict).build() ;
+            // Dictionary<string, object> dict = await azureBlobDBRepository.GetBlobSasUri(request.@params,true);
+           // dict.Add(d.Key, d.Value);
+            return builder.Data(await azureBlobDBRepository.GetContainerSasUri(request.@params, true)).build() ;
         }
         /// <summary>
         /// 某个文件的上传SAS rcw权限
@@ -46,12 +45,13 @@ namespace TEAMModelOS.Controllers.Core
         /// <returns></returns>
         [HttpPost("blobSasRCW")]
         public async Task<BaseJosnRPCResponse> BlobSasRCW(JosnRPCRequest<BlobSas> request)
-        {///返回金钥过期时间
+        {
+            ///返回金钥过期时间
             JsonRPCResponseBuilder builder = new JsonRPCResponseBuilder();
-            Dictionary<string,object> dict=  await azureBlobDBRepository.GetBlobSasUri(request.@params,false);
-            KeyValuePair<string, object> d = await azureBlobDBRepository.GetContainerSasUri(request.@params, false);
-            dict.Add(d.Key, d.Value);
-            return builder.Data(dict).build();
+            // Dictionary<string,object> dict=  await azureBlobDBRepository.GetBlobSasUri(request.@params,false);
+         //   Dictionary<string, object> dict = ;
+            //dict.Add(d.Key, d.Value);
+            return builder.Data(await azureBlobDBRepository.GetContainerSasUri(request.@params, false)).build();
         }
         /// <summary>
         /// 链接只读(读)
@@ -69,8 +69,7 @@ namespace TEAMModelOS.Controllers.Core
             bool flg = IsBlobName(BlobName);
             if (flg)
             {
-                string SAS =await azureBlobDBRepository.GetBlobSasUriRead(ContainerName, BlobName);
-                return responseBuilder.Data(SAS).build();
+                return responseBuilder.Data(await azureBlobDBRepository.GetBlobSasUriRead(ContainerName, BlobName)).build();
             }
             else {
                 throw new BizException("文件名错误", ResponseCode.PARAMS_ERROR);

+ 5 - 5
TEAMModelOS/appsettings.Development.json

@@ -19,15 +19,15 @@
     },
     "Blob": {
       "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=teammodelstorage;AccountKey=Yq7D4dE6cFuer2d2UZIccTA/i0c3sJ/6ITc8tNOyW+K5f+/lWw9GCos3Mxhj47PyWQgDL8YbVD63B9XcGtrMxQ==;EndpointSuffix=core.chinacloudapi.cn",
-      "Container": "teammodelos",
+       "Container": "teammodelos"
       //系统管理员 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
-      "system": [ "res", "item", "htex", "task", "info", "room", "exam" ],
+     // "system": [ "res", "item", "htex", "task", "info", "room", "exam" ],
       //资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
-      "school": [ "res", "item", "htex", "task", "info", "room", "exam" ],
+      //"school": [ "res", "item", "htex", "task", "info", "room", "exam" ],
       //资源,题目关联,htex关联,学习活动关联,教师基本信息关联
-      "teacher": [ "res", "item", "htex", "task", "info" ],
+      //"teacher": [ "res", "item", "htex", "task", "info" ],
       //答案及学习活动上传的文件,学生基本信息关联
-      "student": [ "ans/{studentId}", "stu/{studentId}" ]
+     // "student": [ "stu/{studentId}/ans", "stu/{studentId}/task" ]
     },
     "CosmosDB": {
       "ConnectionString": "https://teammodelos.documents.azure.cn:443/",

+ 5 - 5
TEAMModelOS/appsettings.json

@@ -19,15 +19,15 @@
     },
     "Blob": {
       "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=teammodelstorage;AccountKey=Yq7D4dE6cFuer2d2UZIccTA/i0c3sJ/6ITc8tNOyW+K5f+/lWw9GCos3Mxhj47PyWQgDL8YbVD63B9XcGtrMxQ==;EndpointSuffix=core.chinacloudapi.cn",
-      "Container": "teammodelos",
+      "Container": "teammodelos"
       //系统管理员 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
-      "system": [ "res", "item", "htex", "task", "info", "room", "exam" ],
+      //"system": [ "res", "item", "htex", "task", "info", "room", "exam" ],
       //资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
-      "school": [ "res", "item", "htex", "task", "info", "room", "exam" ],
+     // "school": [ "res", "item", "htex", "task", "info", "room", "exam" ],
       //资源,题目关联,htex关联,学习活动关联,教师基本信息关联
-      "teacher": [ "res", "item", "htex", "task", "info" ],
+     // "teacher": [ "res", "item", "htex", "task", "info" ],
       //答案及学习活动上传的文件,学生基本信息关联
-      "student": [ "ans/{studentId}", "stu/{studentId}" ]
+      //"student": [ "ans/{studentId}", "stu/{studentId}" ]
     },
     "CosmosDB": {
       "ConnectionString": "https://teammodelos.documents.azure.cn:443/",