AzureStorageBlobExtensions.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. namespace TEAMModelOS.SDK.DI
  17. {
  18. public static class AzureStorageBlobExtensions
  19. {
  20. /// <summary>
  21. /// 取得指定前置詞的 Blob 名稱的總大小(Bytes),例如指定目錄名稱為前置詞
  22. /// </summary>
  23. /// <param name="prefix">篩選開頭名稱,Null代表容器總大小</param>
  24. /// <returns>總大小(Bytes),如果為Null代表查無前置詞或者發生錯誤</returns>
  25. public static async Task<long?> GetBlobsSize(this BlobContainerClient client, string prefix = null)
  26. {
  27. long? size = 0;
  28. try
  29. {
  30. await foreach (BlobItem item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix))
  31. {
  32. size += item.Properties.ContentLength;
  33. };
  34. return size;
  35. }
  36. catch
  37. {
  38. return size;
  39. }
  40. }
  41. /// <summary>
  42. /// 取得指定前置詞的 Blob 名稱的總大小(Bytes),例如指定目錄名稱為前置詞
  43. /// </summary>
  44. /// <param name="urls">多个文件的连接/param>
  45. /// <returns>總大小(Bytes),如果為Null代表查無前置詞或者發生錯誤</returns>
  46. public static async Task<long?> GetBlobsSize(this BlobContainerClient client, List<string>urls)
  47. {
  48. long? size = 0;
  49. try
  50. {
  51. foreach (var url in urls) {
  52. var eurl = System.Web.HttpUtility.UrlDecode(url.ToString(), Encoding.UTF8);
  53. var blob = client.GetBlobClient(eurl);
  54. if (blob.Exists()) {
  55. var props = await blob.GetPropertiesAsync();
  56. size += props.Value.ContentLength;
  57. }
  58. }
  59. return size;
  60. }
  61. catch
  62. {
  63. return size;
  64. }
  65. }
  66. /// <summary>
  67. /// 批量刪除Blobs
  68. /// </summary>
  69. /// <param name="prefix">篩選開頭名稱,Null代表容器</param>
  70. public static async Task<bool> DelectBlobs(this BlobServiceClient client, string blobContainerName, List<Uri> blobs = null)
  71. {
  72. try
  73. {
  74. BlobContainerClient bcc = client.GetBlobContainerClient(blobContainerName);
  75. BlobBatchClient bbc = client.GetBlobBatchClient();
  76. if (blobs.Count <= 256)
  77. {
  78. await bbc.DeleteBlobsAsync(blobs);
  79. return true;
  80. }
  81. else
  82. {
  83. int pages = (blobs.Count + 255) / 256; //256是批量操作最大值,pages = (total + max -1) / max;
  84. for (int i = 0; i < pages; i++)
  85. {
  86. List<Uri> lists = blobs.Skip((i) * 256).Take(256).ToList();
  87. await bbc.DeleteBlobsAsync(lists);
  88. }
  89. return true;
  90. }
  91. }
  92. catch
  93. {
  94. return false;
  95. }
  96. }
  97. /// <summary>
  98. /// 批量刪除Blobs
  99. /// </summary>
  100. /// <param name="prefix">篩選開頭名稱,Null代表容器</param>
  101. public static async Task<bool> DelectBlobs(this BlobServiceClient client, string blobContainerName, string prefix = null)
  102. {
  103. if (string.IsNullOrWhiteSpace(prefix)) return false;
  104. try
  105. {
  106. BlobContainerClient bcc = client.GetBlobContainerClient(blobContainerName);
  107. BlobBatchClient bbc = client.GetBlobBatchClient();
  108. List<Uri> blobs = new List<Uri>();
  109. await foreach (var item in bcc.GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix))
  110. {
  111. var urib = new UriBuilder(bcc.Uri);
  112. urib.Path += "/" + item.Name;
  113. blobs.Add(urib.Uri);
  114. };
  115. if (blobs.Count <= 256)
  116. {
  117. await bbc.DeleteBlobsAsync(blobs);
  118. return true;
  119. }
  120. else
  121. {
  122. int pages = (blobs.Count + 255) / 256; //256是批量操作最大值,pages = (total + max -1) / max;
  123. for (int i = 0; i < pages; i++)
  124. {
  125. List<Uri> lists = blobs.Skip((i) * 256).Take(256).ToList();
  126. await bbc.DeleteBlobsAsync(lists);
  127. }
  128. return true;
  129. }
  130. }
  131. catch
  132. {
  133. return false;
  134. }
  135. }
  136. /// <summary>
  137. /// 系统管理员 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
  138. /// "system": [ "res", "item", "htex", "task", "info", "room", "exam" ],
  139. /// 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
  140. /// "school": [ "res", "item", "htex", "task", "info", "room", "exam" ],
  141. /// 资源,题目关联,htex关联,学习活动关联,教师基本信息关联
  142. /// "teacher": [ "res", "item", "htex", "task", "info" ],
  143. /// 答案及学习活动上传的文件,学生基本信息关联
  144. ///"student": [ "stu/{studentId}/ans", "stu/{studentId}/task" ]
  145. /// </summary>
  146. /// <param name="name">容器名称</param>
  147. /// <param name="json">文件内容的流</param>
  148. /// <param name="folder">业务文件夹</param>
  149. /// <param name="fileName">文件名</param>
  150. /// <param name="contentTypeDefault">是否存放文件后缀对应的contentType</param>
  151. /// <returns></returns>
  152. public static async Task<string> UploadFileByContainer(this AzureStorageFactory azureStorage, string name, string json, string root , string blobpath, bool contentTypeDefault = true)
  153. {
  154. // string groupName =folder;
  155. BlobContainerClient blobContainer = azureStorage.GetBlobContainerClient(name.ToLower().Replace("#", "")); //blobClient.GetContainerReference(groupName);
  156. var blockBlob = blobContainer.GetBlobClient($"{root}/{blobpath}");
  157. string content_type = "application/octet-stream";
  158. if (!contentTypeDefault)
  159. {
  160. string fileext = blobpath.Substring(blobpath.LastIndexOf(".") > 0 ? blobpath.LastIndexOf(".") : 0);
  161. ContentTypeDict.dict.TryGetValue(fileext, out string contenttype);
  162. if (!string.IsNullOrEmpty(contenttype))
  163. {
  164. content_type = contenttype;
  165. }
  166. }
  167. byte[] bytes = System.Text.Encoding.Default.GetBytes(json);
  168. Stream streamBlob = new MemoryStream(bytes);
  169. await blockBlob.UploadAsync(streamBlob, true);
  170. blockBlob.SetHttpHeaders(new BlobHttpHeaders { ContentType = content_type });
  171. return blockBlob.Uri.ToString();
  172. }
  173. /// <summary>
  174. /// 系统管理员 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
  175. /// "system": [ "res", "item", "htex", "task", "info", "room", "exam" ],
  176. /// 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
  177. /// "school": [ "res", "item", "htex", "task", "info", "room", "exam" ],
  178. /// 资源,题目关联,htex关联,学习活动关联,教师基本信息关联
  179. /// "teacher": [ "res", "item", "htex", "task", "info" ],
  180. /// 答案及学习活动上传的文件,学生基本信息关联
  181. ///"student": [ "stu/{studentId}/ans", "stu/{studentId}/task" ]
  182. /// </summary>
  183. /// <param name="name">容器名称</param>
  184. /// <param name="stream">文件内容的流</param>
  185. /// <param name="folder">业务文件夹</param>
  186. /// <param name="fileName">文件名</param>
  187. /// <param name="contentTypeDefault">是否存放文件后缀对应的contentType</param>
  188. /// <returns></returns>
  189. public static async Task<string> UploadFileByContainer(this AzureStorageFactory azureStorage, string name, Stream stream, string root, string blobpath, bool contentTypeDefault = true)
  190. {
  191. BlobContainerClient blobContainer = azureStorage.GetBlobContainerClient(name.ToLower().Replace("#", "")); //blobClient.GetContainerReference(groupName);
  192. Uri url = blobContainer.Uri;
  193. var blockBlob = blobContainer.GetBlobClient($"{root}/{blobpath}");
  194. string content_type = "application/octet-stream";
  195. if (!contentTypeDefault)
  196. {
  197. string fileext = blobpath.Substring(blobpath.LastIndexOf(".") > 0 ? blobpath.LastIndexOf(".") : 0);
  198. ContentTypeDict.dict.TryGetValue(fileext, out string contenttype);
  199. if (!string.IsNullOrEmpty(contenttype))
  200. {
  201. content_type = contenttype;
  202. }
  203. }
  204. await blockBlob.UploadAsync(stream, true);
  205. blockBlob.SetHttpHeaders(new BlobHttpHeaders { ContentType = content_type });
  206. return blockBlob.Uri.ToString();
  207. }
  208. }
  209. }