AzureStorageBlobExtensions.cs 8.3 KB

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