ソースを参照

处理blob容器计算问题

CrazyIter_Bin 3 年 前
コミット
a221d46298

+ 42 - 5
TEAMModelOS.SDK/DI/AzureStorage/AzureStorageBlobExtensions.cs

@@ -34,7 +34,24 @@ namespace TEAMModelOS.SDK.DI
             {
                 await foreach (BlobItem item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix))
                 {
-                    size += item.Properties.ContentLength;
+
+                    if (item.Name.StartsWith("res/", StringComparison.OrdinalIgnoreCase) && item.Name.EndsWith(".htex", StringComparison.OrdinalIgnoreCase))
+                    { continue; }
+                    if (!prefix.Equals(item.Name))
+                    {
+                        //避免操作(1111) /1111/1111.json  /1111111/11111.json
+                        if (!prefix.EndsWith("/"))
+                        {
+                            if (item.Name.StartsWith(prefix + "/"))
+                            {
+                                size += item.Properties.ContentLength;
+                            }
+                        }
+                    }
+                    else
+                    {
+                        size += item.Properties.ContentLength;
+                    }
                 };
                 return size;
             }
@@ -141,17 +158,18 @@ namespace TEAMModelOS.SDK.DI
                 BlobContainerClient bcc = client.GetBlobContainerClient(blobContainerName);
                 BlobBatchClient bbc = client.GetBlobBatchClient();
                 List<Uri> blobs = new List<Uri>();
+                List<Task<Azure.Response<bool>>> list = new List<Task<Response<bool>>>();
                 foreach (var prefix in prefixs) {
                     string px = prefix;
                     if (prefix.StartsWith("/")) {
                         px= prefix.Substring(1);
                     }
                     var items = bcc.GetBlobsAsync(BlobTraits.None, BlobStates.None, px);
+                   
                     await foreach (var item in items)
                     {
-                        Console.WriteLine(item.Name);
                         var urib = new UriBuilder(bcc.Uri);
-                       await bcc.GetBlobClient(item.Name).DeleteIfExistsAsync();
+                        
                         if (!prefix.Equals(item.Name))
                         {
                             //避免操作(1111) /1111/1111.json  /1111111/11111.json
@@ -159,18 +177,36 @@ namespace TEAMModelOS.SDK.DI
                             {
                                 if (item.Name.StartsWith(prefix+ "/")) {
                                     string path =$"{ urib.Uri.AbsoluteUri }/{ item.Name}";
+                                    list.Add(bcc.GetBlobClient(item.Name).DeleteIfExistsAsync());
+                                    if (item.Name.StartsWith("res/") && item.Name.EndsWith("/index.json")) {
+                                        list.Add(bcc.GetBlobClient($"{prefix}.htex").DeleteIfExistsAsync());
+                                        list.Add(bcc.GetBlobClient($"{prefix}.HTEX").DeleteIfExistsAsync());
+                                    }
                                     blobs.Add(new Uri(path));
                                 }
                             }
                         }
                         else {
                             string path = $"{ urib.Uri.AbsoluteUri }/{ item.Name}";
+                            list.Add(bcc.GetBlobClient(item.Name).DeleteIfExistsAsync());
                             blobs.Add(new Uri(path));
                         }
-                      
-                        
                     };
                 }
+                if (list.Count <= 256)
+                {
+                    await Task.WhenAll(list);
+                }
+                else {
+                    int pages = (list.Count + 255) / 256; //256是批量操作最大值,pages = (total + max -1) / max;
+                    for (int i = 0; i < pages; i++)
+                    {
+                        List<Task<Azure.Response<bool>>> lists = list.Skip((i) * 256).Take(256).ToList();
+                        await Task.WhenAll(lists);
+                    }
+                }
+                return true;
+                /*
                 if (blobs.Count <= 256)
                 {
                     if (blobs.Count > 0) {
@@ -217,6 +253,7 @@ namespace TEAMModelOS.SDK.DI
                     }
                     return true;
                 }
+                */
             }
             catch(Exception ex )
             {

+ 15 - 2
TEAMModelOS/Controllers/Core/BlobController.cs

@@ -450,7 +450,16 @@ namespace TEAMModelOS.Controllers.Core
                         u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
                         root.Add(u);
                     }
-                    var size = await _azureStorage.GetBlobContainerClient($"{name}").GetBlobsSize(url);
+                    long? size = 0;
+                    if (url.StartsWith("res/") && url.EndsWith("/index.json"))
+                    {
+                        var prefix=  url.Substring(0, url.Length - 11);
+                        size = await _azureStorage.GetBlobContainerClient($"{name}").GetBlobsSize(prefix);
+                    }
+                    else {
+                        size = await _azureStorage.GetBlobContainerClient($"{name}").GetBlobsSize(url);
+                    }
+                    
                     long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                     //地址相同的,直接更新
                     bool exsit = false;
@@ -657,10 +666,14 @@ namespace TEAMModelOS.Controllers.Core
                     List<string> deleteUrl = new List<string>();
                     urls.ForEach(x => {
                         string delUrl = x;
-                        if (x.StartsWith("res") && x.EndsWith(".HTEX", StringComparison.OrdinalIgnoreCase))
+                        if (x.StartsWith("res/") && x.EndsWith(".HTEX", StringComparison.OrdinalIgnoreCase))
                         {
                             delUrl = x.Substring(0, x.Length - 4);
                         }
+                        if (x.StartsWith("res/") && x.EndsWith("/index.json", StringComparison.OrdinalIgnoreCase))
+                        {
+                            delUrl = x.Substring(0, x.Length -11);
+                        }
                         //自动删除视频和图片的缩略图
                         if (x.StartsWith("image")||x.StartsWith("video")) {
 

+ 5 - 5
TEAMModelOS/appsettings.Development.json

@@ -22,11 +22,11 @@
   "Azure": {
     //
     "Storage": {
-     // "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=teammodelstorage;AccountKey=Yq7D4dE6cFuer2d2UZIccTA/i0c3sJ/6ITc8tNOyW+K5f+/lWw9GCos3Mxhj47PyWQgDL8YbVD63B9XcGtrMxQ==;EndpointSuffix=core.chinacloudapi.cn"
-      "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=teammodelos;AccountKey=Dl04mfZ9hE9cdPVO1UtqTUQYN/kz/dD/p1nGvSq4tUu/4WhiKcNRVdY9tbe8620nPXo/RaXxs+1F9sVrWRo0bg==;EndpointSuffix=core.chinacloudapi.cn"
+      //"ConnectionString": "DefaultEndpointsProtocol=https;AccountName=teammodelstorage;AccountKey=Yq7D4dE6cFuer2d2UZIccTA/i0c3sJ/6ITc8tNOyW+K5f+/lWw9GCos3Mxhj47PyWQgDL8YbVD63B9XcGtrMxQ==;EndpointSuffix=core.chinacloudapi.cn"
+     "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=teammodelos;AccountKey=Dl04mfZ9hE9cdPVO1UtqTUQYN/kz/dD/p1nGvSq4tUu/4WhiKcNRVdY9tbe8620nPXo/RaXxs+1F9sVrWRo0bg==;EndpointSuffix=core.chinacloudapi.cn"
     },
     "Cosmos": {
-      //"ConnectionString": "AccountEndpoint=https://cdhabookdep-free.documents.azure.cn:443/;AccountKey=JTUVk92Gjsx17L0xqxn0X4wX2thDPMKiw4daeTyV1HzPb6JmBeHdtFY1MF1jdctW1ofgzqkDMFOtcqS46by31A==;"
+      // "ConnectionString": "AccountEndpoint=https://cdhabookdep-free.documents.azure.cn:443/;AccountKey=JTUVk92Gjsx17L0xqxn0X4wX2thDPMKiw4daeTyV1HzPb6JmBeHdtFY1MF1jdctW1ofgzqkDMFOtcqS46by31A==;"
       "ConnectionString": "AccountEndpoint=https://teammodelos.documents.azure.cn:443/;AccountKey=clF73GwPECfP1lKZTCvs8gLMMyCZig1HODFbhDUsarsAURO7TcOjVz6ZFfPqr1HzYrfjCXpMuVD5TlEG5bFGGg==;"
     },
     "Redis": {
@@ -35,8 +35,8 @@
     },
     "ServiceBus": {
       "ConnectionString": "Endpoint=sb://teammodelos.servicebus.chinacloudapi.cn/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=Sy4h4EQ8zP+7w/lOLi1X3tGord/7ShFHimHs1vC50Dc=",
-      "ActiveTask": "dep-active-task",
-      "NoticeTask": "dep-notice-task"
+      "ActiveTask": "active-task",
+      "NoticeTask": "notice-task"
     }
   },
   "HaBookAuth": {