AzureBlobDBRepository.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
  2. using TEAMModelOS.SDK.Module.AzureBlob.Container;
  3. using TEAMModelOS.SDK.Module.AzureBlob.Interfaces;
  4. using Microsoft.WindowsAzure.Storage;
  5. using Microsoft.WindowsAzure.Storage.Blob;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Net.Http.Headers;
  10. using System.Threading.Tasks;
  11. using Microsoft.AspNetCore.Http.Internal;
  12. using TEAMModelOS.SDK.Helper.Security.AESCrypt;
  13. using TEAMModelOS.SDK.Context.Exception;
  14. using Microsoft.AspNetCore.Http;
  15. using System.Linq;
  16. using Microsoft.WindowsAzure.Storage.Shared.Protocol;
  17. using TEAMModelOS.SDK.Extension.SnowFlake;
  18. namespace TEAMModelOS.SDK.Module.AzureBlob.Implements
  19. {
  20. public class AzureBlobDBRepository : IAzureBlobDBRepository
  21. {
  22. private readonly string china = "417A7572654368696E612020202020202020202020202020202020202020202097EB27FCC1F03349787DCD35F4DE22BBDFEDC90F24738B1D7FB9167A2C191BE671B512E17D48B73A002FC98867345CD59D3250AF59FD5FDFFC67976108F9E3BC9E9F75EDE605B058C1821D16BD9EB753B8E7D39FF48163430C1B5F3B6150195B880C3FCB87D35BF3540432734B768EC28C77B4CF0D556E794DE57979C1E01C429E66F7B2794D9940CF287F2B22A22E5F266B949D5E523E709FF37229E45D1A8FC8C4341E0A8346BB976CCB3D91802FFE5A4A28577898B4E942B5BA3A4A7B796FA673782D405060E7F2CBA4F67DF59F47";
  23. private readonly string global = "417A757265476C6F62616C2020202020202020202020202020202020202020206956019D195ED330AFA660D369B9464FC5E90AB3A106FDDD7978A2772DB186CDAE21C6CBFDE2B6739F089E853B3171A27841026E61C51666347F63FDF63E4377448D493B05CF6CDB3791946B9145825DD7756392EB8EA36DBF42E5C1C0021CEC2CDB5F4EA57EBCFA98B17D7236FA2CDCA6E7FCBE1DDC45BEAF691A2462A8BC3C429CBC4BCCA3192E554D23758AA8EA5937F988C927534C70A4769ED33878BEC10E2550F121E4AEB5A2DA213F2902D602A758C7D93D5DED368544F8A86D2A0CAA7813D1D950EC81D544EE41A8EDC84173";
  24. public CloudBlobClient blobClient;
  25. public CloudBlobContainer blobContainer;
  26. public AzureBlobOptions _options;
  27. public AzureBlobDBRepository(AzureBlobOptions options)
  28. {
  29. _options = options;
  30. if (!string.IsNullOrEmpty(options.ConnectionString))
  31. {
  32. blobClient = BlobClientSingleton.getInstance(options.ConnectionString).GetBlobClient();
  33. }
  34. else if (AzureBlobConfig.AZURE_CHINA.Equals(options.AzureTableDialect))
  35. {
  36. AESCrypt crypt = new AESCrypt();
  37. blobClient = BlobClientSingleton.getInstance(crypt.Decrypt(china, options.AzureTableDialect)).GetBlobClient();
  38. }
  39. else if (AzureBlobConfig.AZURE_GLOBAL.Equals(options.AzureTableDialect))
  40. {
  41. AESCrypt crypt = new AESCrypt();
  42. blobClient = BlobClientSingleton.getInstance(crypt.Decrypt(global, options.AzureTableDialect)).GetBlobClient();
  43. }
  44. else { throw new BizException("请设置正确的AzureBlob文件存储配置信息!"); }
  45. }
  46. public AzureBlobDBRepository()
  47. {
  48. // _connectionString = BaseConfigModel.Configuration["AppSettings:Azure:TableStorageConnection"];
  49. }
  50. //private async Task InitializeBlob(string container)
  51. //{
  52. ////https://teammodelstorage.blob.core.chinacloudapi.cn/wechatfilescontainer
  53. // if (blobContainer == null)
  54. // {
  55. // // Type t = typeof(T);
  56. // //若要将权限设置为仅针对 blob 的公共读取访问,请将 PublicAccess 属性设置为 BlobContainerPublicAccessType.Blob。
  57. // //要删除匿名用户的所有权限,请将该属性设置为 BlobContainerPublicAccessType.Off。
  58. // blobContainer = blobClient.GetContainerReference("wechatfilescontainer");
  59. // // await blobContainer.CreateIfNotExistsAsync();
  60. // // BlobContainerPermissions permissions = await blobContainer.GetPermissionsAsync();
  61. // // permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
  62. // // await blobContainer.SetPermissionsAsync(permissions);
  63. // }
  64. // //await UploadFiles(null, new FileContainer() );
  65. //}
  66. public async Task<List<AzureBlobModel>> UploadFiles(IFormFile[] file,string fileSpace="common")
  67. {
  68. string groupName = fileSpace+"/" +DateTime.Now.ToString("yyyyMMdd");
  69. string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss");
  70. // await InitializeBlob(DateTime.Now.ToString("yyyyMMdd"));
  71. blobContainer = blobClient.GetContainerReference( groupName);
  72. //var serviceProperties = await blobClient.GetServicePropertiesAsync();
  73. //var corsSettings = serviceProperties.Cors;
  74. //var corsRule = corsSettings.CorsRules.FirstOrDefault(
  75. // o => o.AllowedOrigins.Contains("http://localhost:3904"));//设置你自己的服务器地址
  76. //if (corsRule == null)
  77. //{
  78. // //Add a new rule.
  79. // corsRule = new CorsRule()
  80. // {
  81. // AllowedHeaders = new List<string> { "x-ms-*", "content-type", "accept" },
  82. // AllowedMethods = CorsHttpMethods.Put| CorsHttpMethods.Head | CorsHttpMethods.Post | CorsHttpMethods.Merge | CorsHttpMethods.Get,//Since we'll only be calling Put Blob, let's just allow PUT verb
  83. // AllowedOrigins = new List<string> { "http://localhost:3904" },//This is the URL of our application.
  84. // ExposedHeaders = { },
  85. // MaxAgeInSeconds = 1 * 60 * 60,//Let the browswer cache it for an hour
  86. // };
  87. // corsSettings.CorsRules.Add(corsRule);
  88. // //Save the rule
  89. // await blobClient.SetServicePropertiesAsync(serviceProperties);
  90. //}
  91. StorageUri url = blobContainer.StorageUri;
  92. List<AzureBlobModel> list = new List<AzureBlobModel>();
  93. foreach (FormFile f in file)
  94. {
  95. string[] names = f.FileName.Split(".");
  96. string name = "";
  97. for (int i = 0; i < names.Length-1; i++) {
  98. name = name + names[i];
  99. }
  100. if (names.Length <= 1)
  101. {
  102. name = name + "_" + newFileName;
  103. }
  104. else {
  105. name = name + "_" + newFileName + "." + names[names.Length - 1];
  106. }
  107. var parsedContentDisposition = ContentDispositionHeaderValue.Parse(f.ContentDisposition);
  108. var filename = Path.Combine(parsedContentDisposition.FileName.Trim('"'));
  109. var blockBlob = blobContainer.GetBlockBlobReference(name);
  110. await blockBlob.UploadFromStreamAsync(f.OpenReadStream());
  111. AzureBlobModel model = new AzureBlobModel(f, _options.Container, groupName, name)
  112. {
  113. BlobUrl = url.PrimaryUri.ToString().Split("?")[0] + "/" + name
  114. };
  115. list.Add(model);
  116. }
  117. return list;
  118. }
  119. public async Task<AzureBlobModel> UploadWord(IFormFile file, string fileSpace = "wordfiles")
  120. {
  121. long bizno= IdWorker.getInstance().NextId();
  122. string groupName = fileSpace + "/" + DateTime.Now.ToString("yyyyMMdd")+"/"+ bizno;
  123. string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss");
  124. // await InitializeBlob(DateTime.Now.ToString("yyyyMMdd"));
  125. blobContainer = blobClient.GetContainerReference(groupName);
  126. StorageUri url = blobContainer.StorageUri;
  127. string[] names = file.FileName.Split(".");
  128. string name = "";
  129. for (int i = 0; i < names.Length - 1; i++)
  130. {
  131. name = name + names[i];
  132. }
  133. if (names.Length <= 1)
  134. {
  135. name = name + "_" + newFileName;
  136. }
  137. else
  138. {
  139. name = name + "_" + newFileName + "." + names[names.Length - 1];
  140. }
  141. var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
  142. var filename = Path.Combine(parsedContentDisposition.FileName.Trim('"'));
  143. var blockBlob = blobContainer.GetBlockBlobReference(name);
  144. await blockBlob.UploadFromStreamAsync(file.OpenReadStream());
  145. AzureBlobModel model = new AzureBlobModel(file, _options.Container, groupName, name)
  146. {
  147. BlobUrl = url.PrimaryUri.ToString().Split("?")[0] + "/" + name
  148. };
  149. return model;
  150. }
  151. }
  152. }