OssHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Threading;
  3. using System.Linq;
  4. using System.IO;
  5. using Aliyun.OSS;
  6. using Aliyun.OSS.Common;
  7. using System.Text.RegularExpressions;
  8. using Common;
  9. using Infrastructure;
  10. using Infrastructure.Model;
  11. namespace Util
  12. {
  13. public class OssHelper
  14. {
  15. public string key = ""; //阿里云AccessKey ID
  16. public string secret = ""; //阿里云Access Key Secret
  17. public string PathName = "Omega";
  18. public string endpoint = ""; //endpoint
  19. public string bucketName = "";
  20. public string SourceHost = "";
  21. public bool OssStatus = false;
  22. public readonly static OssHelper Instance = new OssHelper();
  23. private OssHelper()
  24. {
  25. var options = App.OptionsSetting;
  26. OssConfigs ossConfigs = options.OssConfigs;
  27. key = ossConfigs.Key;
  28. secret = ossConfigs.Secret;
  29. endpoint = ossConfigs.Endpoint;
  30. bucketName = ossConfigs.BucketName;
  31. SourceHost = "https://" + ossConfigs.BucketName + "." + ossConfigs.Endpoint + "/";
  32. }
  33. /// <summary>
  34. /// 上传文件
  35. /// </summary>
  36. /// <returns></returns>
  37. public void Upload(string dataFilePath)
  38. {
  39. var client = new OssClient(OssHelper.Instance.endpoint, OssHelper.Instance.key, OssHelper.Instance.secret);
  40. if(dataFilePath.Substring(dataFilePath.LastIndexOf("/")).Contains("."))
  41. {
  42. if(dataFilePath.Contains("net7.0/"))
  43. {
  44. string localFile = dataFilePath.Substring(dataFilePath.IndexOf("net7.0//") + 8).Replace("\\", "/");
  45. ScanQueue(localFile, dataFilePath, client);
  46. }
  47. else
  48. {
  49. string localFile = dataFilePath.Substring(dataFilePath.IndexOf("omega_make/") + 11).Replace("\\", "/");
  50. ScanQueue(localFile, dataFilePath, client);
  51. }
  52. }
  53. else
  54. {
  55. System.IO.FileSystemInfo info = new System.IO.DirectoryInfo(dataFilePath);
  56. scan(info, client);
  57. }
  58. }
  59. private void scan(System.IO.FileSystemInfo info, OssClient client)
  60. {
  61. if (!info.Exists) return;
  62. System.IO.DirectoryInfo dir = info as System.IO.DirectoryInfo;
  63. //不是目录
  64. if (dir == null) return;
  65. System.IO.FileSystemInfo[] files = dir.GetFileSystemInfos();
  66. for (int i = 0; i < files.Length; i++)
  67. {
  68. System.IO.FileInfo file = files[i] as System.IO.FileInfo;
  69. //是文件
  70. if (file != null)
  71. {
  72. if(file.FullName.Contains("net7.0/"))
  73. {
  74. string localFile = file.FullName.Substring(file.FullName.IndexOf("net7.0//") + 8).Replace("\\", "/");
  75. ScanQueue(localFile, file.FullName, client);
  76. }
  77. else
  78. {
  79. string localFile = file.FullName.Substring(file.FullName.IndexOf("omega_make/") + 11).Replace("\\", "/");
  80. ScanQueue(localFile, file.FullName, client);
  81. }
  82. }
  83. else scan(files[i], client);
  84. }
  85. }
  86. //上传
  87. public void ScanQueue(string data, string FileFullName, OssClient client)
  88. {
  89. // 上传文件。
  90. string localFile = Function.getPath(data);
  91. string filePath = data.TrimStart('/');
  92. var result = client.PutObject(bucketName, filePath, localFile, new ObjectMetadata()
  93. {
  94. ExpirationTime = DateTime.Parse("2050-12-31 23:59:59")
  95. });
  96. if (!string.IsNullOrEmpty(result.ETag))
  97. {
  98. if (result.ETag.Length == 32)
  99. {
  100. if (File.Exists(localFile))
  101. {
  102. File.Delete(localFile);
  103. }
  104. }
  105. }
  106. if (File.Exists(FileFullName))
  107. {
  108. File.Delete(FileFullName);
  109. }
  110. }
  111. }
  112. }