AzureStorageBlobExtensions.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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="json">文件内容的流</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<string> UploadFileByContainer(this AzureStorageFactory azureStorage, string name, string json, string root , string blobpath, bool contentTypeDefault = true)
  96. {
  97. // string groupName =folder;
  98. BlobContainerClient blobContainer = azureStorage.GetBlobContainerClient(name.ToLower().Replace("#", "")); //blobClient.GetContainerReference(groupName);
  99. var blockBlob = blobContainer.GetBlobClient($"{root}/{blobpath}");
  100. string content_type = "application/octet-stream";
  101. if (!contentTypeDefault)
  102. {
  103. string fileext = blobpath.Substring(blobpath.LastIndexOf(".") > 0 ? blobpath.LastIndexOf(".") : 0);
  104. ContentTypeDict.dict.TryGetValue(fileext, out string contenttype);
  105. if (!string.IsNullOrEmpty(contenttype))
  106. {
  107. content_type = contenttype;
  108. }
  109. else
  110. {
  111. //blockBlob.Properties.ContentType = content_type;
  112. }
  113. }
  114. else
  115. {
  116. //blockBlob.Properties.ContentType = content_type;
  117. }
  118. byte[] bytes = System.Text.Encoding.Default.GetBytes(json);
  119. Stream streamBlob = new MemoryStream(bytes);
  120. await blockBlob.UploadAsync(streamBlob, true);
  121. blockBlob.SetHttpHeaders(new BlobHttpHeaders { ContentType = content_type });
  122. return blockBlob.Uri.ToString();
  123. }
  124. /// <summary>
  125. /// 系统管理员 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
  126. /// "system": [ "res", "item", "htex", "task", "info", "room", "exam" ],
  127. /// 资源,题目关联,htex关联,学习活动学生上传文件关联,基本信息关联,教室平面图关联,评测冷数据关联
  128. /// "school": [ "res", "item", "htex", "task", "info", "room", "exam" ],
  129. /// 资源,题目关联,htex关联,学习活动关联,教师基本信息关联
  130. /// "teacher": [ "res", "item", "htex", "task", "info" ],
  131. /// 答案及学习活动上传的文件,学生基本信息关联
  132. ///"student": [ "stu/{studentId}/ans", "stu/{studentId}/task" ]
  133. /// </summary>
  134. /// <param name="name">容器名称</param>
  135. /// <param name="stream">文件内容的流</param>
  136. /// <param name="folder">业务文件夹</param>
  137. /// <param name="fileName">文件名</param>
  138. /// <param name="contentTypeDefault">是否存放文件后缀对应的contentType</param>
  139. /// <returns></returns>
  140. public static async Task<AzureBlobModel> UploadFileByContainer(this AzureStorageFactory azureStorage, string name, Stream stream, string folder, string fileName, bool contentTypeDefault = true)
  141. {
  142. // string groupName =folder;
  143. BlobContainerClient blobContainer = azureStorage.GetBlobContainerClient(name.ToLower().Replace("#", "")); //blobClient.GetContainerReference(groupName);
  144. Uri url = blobContainer.Uri;
  145. var blockBlob = blobContainer.GetBlobClient(folder + "/" + fileName);
  146. string fileext = fileName.Substring(fileName.LastIndexOf(".") > 0 ? fileName.LastIndexOf(".") : 0);
  147. string content_type = "application/octet-stream";
  148. if (!contentTypeDefault)
  149. {
  150. ContentTypeDict.dict.TryGetValue(fileext, out string contenttype);
  151. if (!string.IsNullOrEmpty(contenttype))
  152. {
  153. content_type = contenttype;
  154. }
  155. else
  156. {
  157. //blockBlob.Properties.ContentType = content_type;
  158. }
  159. }
  160. else
  161. {
  162. //blockBlob.Properties.ContentType = content_type;
  163. }
  164. await blockBlob.UploadAsync(stream, true);
  165. blockBlob.SetHttpHeaders(new BlobHttpHeaders { ContentType = content_type });
  166. AzureBlobModel model = new AzureBlobModel(fileName, name, folder, fileName, folder, content_type, stream.Length)
  167. {
  168. Sha1Code = ShaHashHelper.GetSHA1(stream),
  169. BlobUrl = blockBlob.Uri.ToString()
  170. };
  171. return model;
  172. }
  173. }
  174. }