AzureStorageBlobExtensions.cs 8.8 KB

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