AzureStorageBlobExtensions.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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> DeleteBlobs(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. if (prefix != item.Name)
  146. {
  147. //避免操作(1111) /1111/1111.json /1111111/11111.json
  148. if (!prefix.EndsWith("/") && item.Name.StartsWith($"{prefix}/"))
  149. {
  150. urib.Path = Path.Combine(urib.Path, item.Name);
  151. blobs.Add(urib.Uri);
  152. }
  153. }
  154. else {
  155. urib.Path = Path.Combine(urib.Path, item.Name);
  156. blobs.Add(urib.Uri);
  157. }
  158. };
  159. }
  160. if (blobs.Count <= 256)
  161. {
  162. if (blobs.Count > 0) {
  163. try { Azure.Response[] ass = await bbc.DeleteBlobsAsync(blobs); }
  164. catch (AggregateException ex)
  165. {
  166. //删除多个时会,如果其中一个文件不存在 或者其中一个删除失败会引发该异常
  167. return true;
  168. }
  169. catch (RequestFailedException ex)
  170. {
  171. return true;
  172. }
  173. catch (Exception ex)
  174. {
  175. return true;
  176. }
  177. }
  178. return true;
  179. }
  180. else
  181. {
  182. int pages = (blobs.Count + 255) / 256; //256是批量操作最大值,pages = (total + max -1) / max;
  183. for (int i = 0; i < pages; i++)
  184. {
  185. List<Uri> lists = blobs.Skip((i) * 256).Take(256).ToList();
  186. try { Azure.Response[] ass = await bbc.DeleteBlobsAsync(lists); }
  187. catch (AggregateException ex)
  188. {
  189. //删除多个时会,如果其中一个文件不存在 或者其中一个删除失败会引发该异常
  190. return true;
  191. }
  192. catch (RequestFailedException ex)
  193. {
  194. return true;
  195. }
  196. catch (Exception ex)
  197. {
  198. return true;
  199. }
  200. }
  201. return true;
  202. }
  203. }
  204. catch(Exception ex )
  205. {
  206. return false;
  207. }
  208. }
  209. /// <summary>
  210. /// 系统管理员 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
  211. /// "system": [ "res", "item", "htex", "task", "info", "room", "exam" ],
  212. /// 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
  213. /// "school": [ "res", "item", "htex", "task", "info", "room", "exam" ],
  214. /// 资源,题目关联,htex关联,学习活动关联,教师基本信息关联
  215. /// "teacher": [ "res", "item", "htex", "task", "info" ],
  216. /// 答案及学习活动上传的文件,学生基本信息关联
  217. ///"student": [ "stu/{studentId}/ans", "stu/{studentId}/task" ]
  218. /// </summary>
  219. /// <param name="name">容器名称</param>
  220. /// <param name="json">文件内容的流</param>
  221. /// <param name="folder">业务文件夹</param>
  222. /// <param name="fileName">文件名</param>
  223. /// <param name="contentTypeDefault">是否存放文件后缀对应的contentType</param>
  224. /// <returns></returns>
  225. public static async Task<string> UploadFileByContainer(this AzureStorageFactory azureStorage, string name, string json, string root , string blobpath, bool contentTypeDefault = true)
  226. {
  227. // string groupName =folder;
  228. BlobContainerClient blobContainer = azureStorage.GetBlobContainerClient(name.ToLower().Replace("#", "")); //blobClient.GetContainerReference(groupName);
  229. var blockBlob = blobContainer.GetBlobClient($"{root}/{blobpath}");
  230. string content_type = "application/octet-stream";
  231. if (!contentTypeDefault)
  232. {
  233. string fileext = blobpath.Substring(blobpath.LastIndexOf(".") > 0 ? blobpath.LastIndexOf(".") : 0);
  234. ContentTypeDict.dict.TryGetValue(fileext, out string contenttype);
  235. if (!string.IsNullOrEmpty(contenttype))
  236. {
  237. content_type = contenttype;
  238. }
  239. }
  240. byte[] bytes = System.Text.Encoding.Default.GetBytes(json);
  241. Stream streamBlob = new MemoryStream(bytes);
  242. await blockBlob.UploadAsync(streamBlob, true);
  243. blockBlob.SetHttpHeaders(new BlobHttpHeaders { ContentType = content_type });
  244. return blockBlob.Uri.ToString();
  245. }
  246. /// <summary>
  247. /// 系统管理员 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
  248. /// "system": [ "res", "item", "htex", "task", "info", "room", "exam" ],
  249. /// 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
  250. /// "school": [ "res", "item", "htex", "task", "info", "room", "exam" ],
  251. /// 资源,题目关联,htex关联,学习活动关联,教师基本信息关联
  252. /// "teacher": [ "res", "item", "htex", "task", "info" ],
  253. /// 答案及学习活动上传的文件,学生基本信息关联
  254. ///"student": [ "stu/{studentId}/ans", "stu/{studentId}/task" ]
  255. /// </summary>
  256. /// <param name="name">容器名称</param>
  257. /// <param name="stream">文件内容的流</param>
  258. /// <param name="folder">业务文件夹</param>
  259. /// <param name="fileName">文件名</param>
  260. /// <param name="contentTypeDefault">是否存放文件后缀对应的contentType</param>
  261. /// <returns></returns>
  262. public static async Task<string> UploadFileByContainer(this AzureStorageFactory azureStorage, string name, Stream stream, string root, string blobpath, bool contentTypeDefault = true)
  263. {
  264. BlobContainerClient blobContainer = azureStorage.GetBlobContainerClient(name.ToLower().Replace("#", "")); //blobClient.GetContainerReference(groupName);
  265. Uri url = blobContainer.Uri;
  266. var blockBlob = blobContainer.GetBlobClient($"{root}/{blobpath}");
  267. string content_type = "application/octet-stream";
  268. if (!contentTypeDefault)
  269. {
  270. string fileext = blobpath.Substring(blobpath.LastIndexOf(".") > 0 ? blobpath.LastIndexOf(".") : 0);
  271. ContentTypeDict.dict.TryGetValue(fileext, out string contenttype);
  272. if (!string.IsNullOrEmpty(contenttype))
  273. {
  274. content_type = contenttype;
  275. }
  276. }
  277. await blockBlob.UploadAsync(stream, true);
  278. blockBlob.SetHttpHeaders(new BlobHttpHeaders { ContentType = content_type });
  279. return blockBlob.Uri.ToString();
  280. }
  281. }
  282. }