AzureStorageBlobExtensions.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System.Threading.Tasks;
  2. using Azure.Storage;
  3. using Azure.Storage.Blobs;
  4. using Azure.Storage.Blobs.Models;
  5. using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
  6. using TEAMModelOS.SDK.Module.AzureBlob.Container;
  7. using TEAMModelOS.SDK.Context.Constant;
  8. using TEAMModelOS.SDK.Helper.Security.ShaHash;
  9. using System;
  10. using System.IO;
  11. using Azure.Storage.Blobs.Specialized;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using Azure.Core;
  16. using Azure;
  17. using TEAMModelOS.SDK;
  18. namespace TEAMModelOS.SDK.DI
  19. {
  20. public static class AzureStorageBlobExtensions
  21. {
  22. /// <summary>
  23. /// 取得指定前置詞的 Blob 名稱的總大小(Bytes),例如指定目錄名稱為前置詞
  24. /// </summary>
  25. /// <param name="prefix">篩選開頭名稱,Null代表容器總大小</param>
  26. /// <returns>總大小(Bytes),如果為Null代表查無前置詞或者發生錯誤</returns>
  27. public static async Task<long?> GetBlobsSize(this BlobContainerClient client, string prefix = null)
  28. {
  29. long? size = 0;
  30. try
  31. {
  32. await foreach (BlobItem item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix))
  33. {
  34. size += item.Properties.ContentLength;
  35. };
  36. return size;
  37. }
  38. catch
  39. {
  40. return size;
  41. }
  42. }
  43. public static async Task<List<string>> List(this BlobContainerClient client, string prefix = null) {
  44. try
  45. {
  46. List<string> items = new List<string>();
  47. await foreach (BlobItem item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix)) {
  48. items.Add(item.Name);
  49. }
  50. return items;
  51. }
  52. catch
  53. {
  54. return null;
  55. }
  56. }
  57. /// <summary>
  58. /// 取得指定前置詞的 Blob 名稱的總大小(Bytes),例如指定目錄名稱為前置詞
  59. /// </summary>
  60. /// <param name="prefix">篩選開頭名稱,Null代表容器總大小</param>
  61. /// <returns>總大小(Bytes),如果為Null代表查無前置詞或者發生錯誤</returns>
  62. public static async Task<(long?, Dictionary<string, double?>)> GetBlobsCatalogSize(this BlobContainerClient client, string prefix = null)
  63. {
  64. long? size = 0;
  65. Dictionary<string, double?> dict = new Dictionary<string, double?>();
  66. try
  67. {
  68. List<KeyValuePair<string, double?>> foderSize = new List<KeyValuePair<string, double?>>();
  69. await foreach (BlobItem item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix))
  70. {
  71. var len = item.Properties.ContentLength;
  72. foderSize.Add(new KeyValuePair<string, double?>(item.Name.Split("/")[0], len));
  73. size += item.Properties.ContentLength;
  74. };
  75. foderSize.Select(x => new { x.Key, x.Value }).GroupBy(y=>y.Key).ToList().ForEach(g=> {
  76. var gpsize = g.Select(m => m.Value).Sum();
  77. dict[g.Key] = gpsize;
  78. });
  79. return (size, dict);
  80. }
  81. catch
  82. {
  83. return (size, dict);
  84. }
  85. }
  86. public class OptUrl
  87. {
  88. public string url { get; set; }
  89. public long size { get; set; }
  90. }
  91. /// <summary>
  92. /// 取得指定前置詞的 Blob 名稱的總大小(Bytes),例如指定目錄名稱為前置詞
  93. /// </summary>
  94. /// <param name="urls">多个文件的连接/param>
  95. /// <returns>總大小(Bytes),如果為Null代表查無前置詞或者發生錯誤</returns>
  96. public static async Task<List<OptUrl>> GetBlobsSize(this BlobContainerClient client, List<string> urls)
  97. {
  98. List<OptUrl> optUrls = new List<OptUrl>();
  99. try
  100. {
  101. if (urls != null) {
  102. foreach (var url in urls)
  103. {
  104. OptUrl optUrl = new OptUrl { url = url, size = 0 };
  105. var eurl = System.Web.HttpUtility.UrlDecode(url, Encoding.UTF8);
  106. var blob = client.GetBlobClient(eurl);
  107. if (blob.Exists())
  108. {
  109. var props = await blob.GetPropertiesAsync();
  110. var size = props.Value.ContentLength;
  111. optUrl.size = size;
  112. }
  113. optUrls.Add(optUrl);
  114. }
  115. }
  116. return optUrls;
  117. }
  118. catch
  119. {
  120. return optUrls;
  121. }
  122. }
  123. /// <summary>
  124. ///prefixs多个文件或文件夹路径删除
  125. ///prefixs 或者按前缀文件夹删除
  126. ///
  127. /// </summary>
  128. /// <param name="prefix">篩選開頭名稱,Null代表容器</param>
  129. public static async Task<bool> DelectBlobs(this BlobServiceClient client, string blobContainerName, List<string> prefixs )
  130. {
  131. if (!prefixs.IsNotEmpty()) return false;
  132. try
  133. {
  134. BlobContainerClient bcc = client.GetBlobContainerClient(blobContainerName);
  135. BlobBatchClient bbc = client.GetBlobBatchClient();
  136. List<Uri> blobs = new List<Uri>();
  137. foreach (var prefix in prefixs) {
  138. string px = prefix;
  139. if (prefix.StartsWith("/")) {
  140. px= prefix.Substring(1);
  141. }
  142. await foreach (var item in bcc.GetBlobsAsync(BlobTraits.None, BlobStates.None, px))
  143. {
  144. var urib = new UriBuilder(bcc.Uri);
  145. urib.Path = Path.Combine(urib.Path, item.Name);
  146. blobs.Add(urib.Uri);
  147. };
  148. }
  149. if (blobs.Count <= 256)
  150. {
  151. if (blobs.Count > 0) {
  152. try { Azure.Response[] ass = await bbc.DeleteBlobsAsync(blobs); }
  153. catch (AggregateException ex)
  154. {
  155. //删除多个时会,如果其中一个文件不存在 或者其中一个删除失败会引发该异常
  156. return true;
  157. }
  158. catch (RequestFailedException ex)
  159. {
  160. return true;
  161. }
  162. catch (Exception ex)
  163. {
  164. return true;
  165. }
  166. }
  167. return true;
  168. }
  169. else
  170. {
  171. int pages = (blobs.Count + 255) / 256; //256是批量操作最大值,pages = (total + max -1) / max;
  172. for (int i = 0; i < pages; i++)
  173. {
  174. List<Uri> lists = blobs.Skip((i) * 256).Take(256).ToList();
  175. try { Azure.Response[] ass = await bbc.DeleteBlobsAsync(lists); }
  176. catch (AggregateException ex)
  177. {
  178. //删除多个时会,如果其中一个文件不存在 或者其中一个删除失败会引发该异常
  179. return true;
  180. }
  181. catch (RequestFailedException ex)
  182. {
  183. return true;
  184. }
  185. catch (Exception ex)
  186. {
  187. return true;
  188. }
  189. }
  190. return true;
  191. }
  192. }
  193. catch(Exception ex )
  194. {
  195. return false;
  196. }
  197. }
  198. /// <summary>
  199. /// 系统管理员 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
  200. /// "system": [ "res", "item", "htex", "task", "info", "room", "exam" ],
  201. /// 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
  202. /// "school": [ "res", "item", "htex", "task", "info", "room", "exam" ],
  203. /// 资源,题目关联,htex关联,学习活动关联,教师基本信息关联
  204. /// "teacher": [ "res", "item", "htex", "task", "info" ],
  205. /// 答案及学习活动上传的文件,学生基本信息关联
  206. ///"student": [ "stu/{studentId}/ans", "stu/{studentId}/task" ]
  207. /// </summary>
  208. /// <param name="name">容器名称</param>
  209. /// <param name="json">文件内容的流</param>
  210. /// <param name="folder">业务文件夹</param>
  211. /// <param name="fileName">文件名</param>
  212. /// <param name="contentTypeDefault">是否存放文件后缀对应的contentType</param>
  213. /// <returns></returns>
  214. public static async Task<string> UploadFileByContainer(this AzureStorageFactory azureStorage, string name, string json, string root , string blobpath, bool contentTypeDefault = true)
  215. {
  216. // string groupName =folder;
  217. BlobContainerClient blobContainer = azureStorage.GetBlobContainerClient(name.ToLower().Replace("#", "")); //blobClient.GetContainerReference(groupName);
  218. var blockBlob = blobContainer.GetBlobClient($"{root}/{blobpath}");
  219. string content_type = "application/octet-stream";
  220. if (!contentTypeDefault)
  221. {
  222. string fileext = blobpath.Substring(blobpath.LastIndexOf(".") > 0 ? blobpath.LastIndexOf(".") : 0);
  223. ContentTypeDict.dict.TryGetValue(fileext, out string contenttype);
  224. if (!string.IsNullOrEmpty(contenttype))
  225. {
  226. content_type = contenttype;
  227. }
  228. }
  229. byte[] bytes = System.Text.Encoding.Default.GetBytes(json);
  230. Stream streamBlob = new MemoryStream(bytes);
  231. await blockBlob.UploadAsync(streamBlob, true);
  232. blockBlob.SetHttpHeaders(new BlobHttpHeaders { ContentType = content_type });
  233. return blockBlob.Uri.ToString();
  234. }
  235. /// <summary>
  236. /// 系统管理员 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
  237. /// "system": [ "res", "item", "htex", "task", "info", "room", "exam" ],
  238. /// 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
  239. /// "school": [ "res", "item", "htex", "task", "info", "room", "exam" ],
  240. /// 资源,题目关联,htex关联,学习活动关联,教师基本信息关联
  241. /// "teacher": [ "res", "item", "htex", "task", "info" ],
  242. /// 答案及学习活动上传的文件,学生基本信息关联
  243. ///"student": [ "stu/{studentId}/ans", "stu/{studentId}/task" ]
  244. /// </summary>
  245. /// <param name="name">容器名称</param>
  246. /// <param name="stream">文件内容的流</param>
  247. /// <param name="folder">业务文件夹</param>
  248. /// <param name="fileName">文件名</param>
  249. /// <param name="contentTypeDefault">是否存放文件后缀对应的contentType</param>
  250. /// <returns></returns>
  251. public static async Task<string> UploadFileByContainer(this AzureStorageFactory azureStorage, string name, Stream stream, string root, string blobpath, bool contentTypeDefault = true)
  252. {
  253. BlobContainerClient blobContainer = azureStorage.GetBlobContainerClient(name.ToLower().Replace("#", "")); //blobClient.GetContainerReference(groupName);
  254. Uri url = blobContainer.Uri;
  255. var blockBlob = blobContainer.GetBlobClient($"{root}/{blobpath}");
  256. string content_type = "application/octet-stream";
  257. if (!contentTypeDefault)
  258. {
  259. string fileext = blobpath.Substring(blobpath.LastIndexOf(".") > 0 ? blobpath.LastIndexOf(".") : 0);
  260. ContentTypeDict.dict.TryGetValue(fileext, out string contenttype);
  261. if (!string.IsNullOrEmpty(contenttype))
  262. {
  263. content_type = contenttype;
  264. }
  265. }
  266. await blockBlob.UploadAsync(stream, true);
  267. blockBlob.SetHttpHeaders(new BlobHttpHeaders { ContentType = content_type });
  268. return blockBlob.Uri.ToString();
  269. }
  270. }
  271. }