Bläddra i källkod

1、统计某个文件夹下所以文件列表

李思淳 5 år sedan
förälder
incheckning
87ad19bba4

+ 17 - 0
TEAMModelOS.SDK/Module/AzureBlob/Configuration/BlobFileDto.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace TEAMModelOS.SDK.Module.AzureBlob.Configuration
+{
+    public class BlobFileDto
+    {
+        public string name { get; set; }
+        public long length { get; set; }
+        public string contentType { get; set; }
+        public long created { get; set; }
+        public long lastModified { get; set; }
+        public long streamWriteSizeInBytes { get; set; }
+        public string url { get; set; }
+    }
+}

+ 15 - 0
TEAMModelOS.SDK/Module/AzureBlob/Configuration/BlockPointDto.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace TEAMModelOS.SDK.Module.AzureBlob.Configuration
+{
+    public class BlockPointDto
+    {
+        public string name { get; set; }
+        public int length { get; set; }
+        public string contentType { get; set; }
+        public DateTimeOffset created { get; set; }
+        public DateTimeOffset lastModified { get; set; }
+    }
+}

+ 45 - 0
TEAMModelOS.SDK/Module/AzureBlob/Implements/AzureBlobDBRepository.cs

@@ -18,6 +18,8 @@ using TEAMModelOS.SDK.Helper.Security.ShaHash;
 using Microsoft.Extensions.Configuration;
 using TEAMModelOS.SDK.Context.Configuration;
 using Microsoft.AspNetCore.Hosting;
+using Microsoft.Azure.Cosmos.Linq;
+using System.Reflection.Metadata;
 
 namespace TEAMModelOS.SDK.Module.AzureBlob.Implements
 {
@@ -652,6 +654,49 @@ namespace TEAMModelOS.SDK.Module.AzureBlob.Implements
             return blob.Uri + sasBlobToken;
         }
 
+        public List<BlobFileDto> GetBlobDirectory(string containerName, string blobName)
+        {
+            blobContainer = GetSASBoloClent(containerName);
+            CloudBlobDirectory cloudBlobDirectory = blobContainer.GetDirectoryReference(blobName);
+
+            BlobContinuationToken blobContinuationToken = new BlobContinuationToken();
+            //var blobResultSegment = cloudBlobDirectory.ListBlobsSegmentedAsync(blobContinuationToken).GetAwaiter().GetResult();
+            //IEnumerable<IListBlobItem> listBlobItems = blobResultSegment.Results;
+            List<BlobFileDto> blobProperties = new List<BlobFileDto>();
+            blobProperties = GetBlobProperties(blobProperties, cloudBlobDirectory);
+            return blobProperties;
+        }
+
+        public List<BlobFileDto> GetBlobProperties(List<BlobFileDto> blobProperties, CloudBlobDirectory blobDirectory) 
+        {
+            BlobContinuationToken blobContinuationToken = new BlobContinuationToken();
+            var blobResultSegment1 = blobDirectory.ListBlobsSegmentedAsync(blobContinuationToken).GetAwaiter().GetResult();
+            IEnumerable<IListBlobItem> listBlobItems1 = blobResultSegment1.Results;
+            foreach (IListBlobItem listBlobItem in listBlobItems1)
+            {
+                if (listBlobItem.GetType() == typeof(CloudBlobDirectory))
+                {
+                    CloudBlobDirectory cloudBlobDirectory = (CloudBlobDirectory)listBlobItem;
+                    blobProperties = GetBlobProperties(blobProperties, cloudBlobDirectory);
+                }
+                else if (listBlobItem.GetType() == typeof(CloudBlockBlob))
+                {
+                    CloudBlockBlob blobaa = (CloudBlockBlob)listBlobItem;
+                    blobaa.FetchAttributesAsync();
+                    BlobFileDto blobFileDto = new BlobFileDto();
+                    blobFileDto.name = blobaa.Name;
+                    blobFileDto.length = blobaa.Properties.Length;
+                    blobFileDto.contentType = blobaa.Properties.ContentType;
+                    blobFileDto.created = blobaa.Properties.Created != null ? blobaa.Properties.Created.Value.ToUnixTimeSeconds():0;//blobaa.Properties.Created.IsNull() ? 0 :
+                    blobFileDto.lastModified = blobaa.Properties.LastModified != null? blobaa.Properties.LastModified.Value.ToUnixTimeSeconds():0;//blobaa.Properties.LastModified.IsNull()? 0:
+                    blobFileDto.streamWriteSizeInBytes = blobaa.StreamWriteSizeInBytes;
+                    blobFileDto.url = blobaa.Uri.AbsoluteUri;
+
+                    blobProperties.Add(blobFileDto);
+                }
+            }
+            return blobProperties;
+        }
 
         public string GetBlobSasUriRead(string containerName, string blobName, string policyName = null)
         {

+ 3 - 0
TEAMModelOS.SDK/Module/AzureBlob/Interfaces/IAzureBlobDBRepository.cs

@@ -4,6 +4,7 @@ using System.Collections.Generic;
 using System.Threading.Tasks;
 using System.IO;
 using Microsoft.WindowsAzure.Storage.Blob;
+using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
 
 namespace TEAMModelOS.SDK.Module.AzureBlob.Interfaces
 {
@@ -24,5 +25,7 @@ namespace TEAMModelOS.SDK.Module.AzureBlob.Interfaces
         Task<bool> CreateSharedAccessPolicyAsync(string policyName , string  containerName = null);
         Task DeleteSharedAccessPolicyAsync(string policyName, string  containerName = null);
         Task Deleteblob(string sasUri);
+
+        List<BlobFileDto> GetBlobDirectory(string containerName, string blobName);
     }
 }

+ 27 - 1
TEAMModelOS/Controllers/Core/FileController.cs

@@ -1,5 +1,6 @@
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
+using Microsoft.WindowsAzure.Storage.Blob;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -10,6 +11,7 @@ using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
 using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
 using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
 using TEAMModelOS.SDK.Helper.Security.AESCrypt;
+using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
 using TEAMModelOS.SDK.Module.AzureBlob.Container;
 using TEAMModelOS.SDK.Module.AzureBlob.Interfaces;
 using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
@@ -131,7 +133,31 @@ namespace TEAMModelOS.Controllers.Syllabus
             }
         }
 
-      
+
+        /// <summary>
+        /// 获取某个文件夹下所以文件列表
+        /// </summary>
+        /// <param name="azureBlobSASDto"></param>
+        /// <returns></returns>
+        [HttpPost("GetBlobDirectory")]
+        public BaseJosnRPCResponse GetBlobDirectory(JosnRPCRequest<string> azureBlobSASDto)
+        {
+            JsonRPCResponseBuilder responseBuilder = new JsonRPCResponseBuilder();
+            string azureBlobSAS = azureBlobSASDto.@params;
+            (string, string) a = BlobUrlString(azureBlobSAS);
+            string ContainerName = a.Item1;
+            string BlobName = a.Item2;
+            bool flg = IsBlobName(BlobName);
+            List<BlobFileDto> blobProperties = new List<BlobFileDto>();
+            if (flg)
+            {
+                string[] blobstring = BlobName.Split("/");
+                //string SAS = _azureBlobDBRepository.GetBlobSasUriRead(ContainerName, BlobName);
+                blobProperties = _azureBlobDBRepository.GetBlobDirectory(ContainerName, blobstring[0]);
+
+            }
+            return responseBuilder.Data(blobProperties).build();
+        }
 
 
 

+ 2 - 2
TEAMModelOS/appsettings.Development.json

@@ -11,8 +11,8 @@
     "Table": {
       "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=teammodelostest;AccountKey=QB/zYHHCAtZfl9tf4emL1Y9ZXGc6fqZ+nNbCxIHM70HnziC8dMdEAu7+Pa4mbKLlbswV90wWHAF3nMjrKB54Lw==;EndpointSuffix=core.chinacloudapi.cn"
     },
-    "Blob": {
-      "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=teammodelstorage;AccountKey=Yq7D4dE6cFuer2d2UZIccTA/i0c3sJ/6ITc8tNOyW+K5f+/lWw9GCos3Mxhj47PyWQgDL8YbVD63B9XcGtrMxQ==;EndpointSuffix=core.chinacloudapi.cn",
+   "Blob": {
+      "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=teammodelostest;AccountKey=QB/zYHHCAtZfl9tf4emL1Y9ZXGc6fqZ+nNbCxIHM70HnziC8dMdEAu7+Pa4mbKLlbswV90wWHAF3nMjrKB54Lw==;EndpointSuffix=core.chinacloudapi.cn", //"DefaultEndpointsProtocol=https;AccountName=teammodelstorage;AccountKey=Yq7D4dE6cFuer2d2UZIccTA/i0c3sJ/6ITc8tNOyW+K5f+/lWw9GCos3Mxhj47PyWQgDL8YbVD63B9XcGtrMxQ==;EndpointSuffix=core.chinacloudapi.cn",
       "Container": "teammodelos"
     },
     "CosmosDB": {