CrazyIter_Bin 4 years ago
parent
commit
ec67d9df4a

+ 3 - 3
TEAMModelFunction/MonitorServicesBus.cs

@@ -155,9 +155,9 @@ namespace TEAMModelFunction
                 var jsonMsg = JsonDocument.Parse(msg);
                 jsonMsg.RootElement.TryGetProperty("code", out JsonElement name);
                 var client = _azureStorage.GetBlobContainerClient($"{name}");
-                var size = await client.GetBlobsSize();
-                await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record",new RedisValue($"{name}"),new RedisValue($"{long.Parse($"{size}")}"));
-                await _dingDing.SendBotMsg($"ServiceBus,Blob() 容器:{name}更新大小:{size}",
+                var size = await client.GetBlobsCatalogSize();
+                await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record",new RedisValue($"{name}"),new RedisValue($"{long.Parse($"{size.Item1}")}"));
+                await _dingDing.SendBotMsg($"ServiceBus,Blob() 容器:{name}更新大小:{size.Item1}",
                         GroupNames.醍摩豆服務運維群組);
             }
             catch (Exception ex)

+ 30 - 0
TEAMModelOS.SDK/DI/AzureStorage/AzureStorageBlobExtensions.cs

@@ -28,6 +28,7 @@ namespace TEAMModelOS.SDK.DI
             long? size = 0;
             try
             {
+               
                 await foreach (BlobItem item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix))
                 {
                     size += item.Properties.ContentLength;
@@ -42,6 +43,35 @@ namespace TEAMModelOS.SDK.DI
         /// <summary>
         /// 取得指定前置詞的 Blob 名稱的總大小(Bytes),例如指定目錄名稱為前置詞
         /// </summary>      
+        /// <param name="prefix">篩選開頭名稱,Null代表容器總大小</param>
+        /// <returns>總大小(Bytes),如果為Null代表查無前置詞或者發生錯誤</returns>
+        public static async Task<(long?, Dictionary<string, long?>)> GetBlobsCatalogSize(this BlobContainerClient client, string prefix = null)
+        {
+            long? size = 0;
+            Dictionary<string, long?> dict = new Dictionary<string, long?>();
+            try
+            {
+                List<KeyValuePair<string, long?>> foderSize = new List<KeyValuePair<string, long?>>();
+                await foreach (BlobItem item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix))
+                {
+                    var len = item.Properties.ContentLength;
+                    foderSize.Add(new KeyValuePair<string, long?>(item.Name.Split("/")[0], len));
+                    size += item.Properties.ContentLength;
+                };
+                foderSize.Select(x => new { x.Key, x.Value }).GroupBy(y=>y.Key).ToList().ForEach(g=> {
+                    var gpsize = g.Select(m => m.Value).Sum();
+                    dict[g.Key] = gpsize;
+                });
+                return (size, dict);
+            }
+            catch
+            {
+                return (size, dict);
+            }
+        }
+        /// <summary>
+        /// 取得指定前置詞的 Blob 名稱的總大小(Bytes),例如指定目錄名稱為前置詞
+        /// </summary>      
         /// <param name="urls">多个文件的连接/param>
         /// <returns>總大小(Bytes),如果為Null代表查無前置詞或者發生錯誤</returns>
         public static async Task<long?> GetBlobsSize(this BlobContainerClient client, List<string>urls)

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

@@ -255,8 +255,10 @@ namespace TEAMModelOS.Controllers.Core
                 }
             } catch { }
             var client = _azureStorage.GetBlobContainerClient(name);
-            var size = await client.GetBlobsSize();
-            await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, size);
+            var size = await client.GetBlobsCatalogSize();
+
+            await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, size.Item1);
+
             return Ok(new { size });
         }
         /// <summary>