OssHelper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. ScanQueue(dataFilePath, dataFilePath, client);
  50. }
  51. }
  52. else
  53. {
  54. System.IO.FileSystemInfo info = new System.IO.DirectoryInfo(dataFilePath);
  55. scan(info, client);
  56. }
  57. }
  58. private void scan(System.IO.FileSystemInfo info, OssClient client)
  59. {
  60. if (!info.Exists) return;
  61. System.IO.DirectoryInfo dir = info as System.IO.DirectoryInfo;
  62. //不是目录
  63. if (dir == null) return;
  64. System.IO.FileSystemInfo[] files = dir.GetFileSystemInfos();
  65. for (int i = 0; i < files.Length; i++)
  66. {
  67. System.IO.FileInfo file = files[i] as System.IO.FileInfo;
  68. //是文件
  69. if (file != null)
  70. {
  71. string localFile = file.FullName.Substring(file.FullName.IndexOf("net7.0//") + 8).Replace("\\", "/");
  72. ScanQueue(localFile, file.FullName, client);
  73. }
  74. else scan(files[i], client);
  75. }
  76. }
  77. //上传
  78. public void ScanQueue(string data, string FileFullName, OssClient client)
  79. {
  80. // 上传文件。
  81. string localFile = Function.getPath(data);
  82. string filePath = data.TrimStart('/');
  83. var result = client.PutObject(bucketName, filePath, localFile, new ObjectMetadata()
  84. {
  85. ExpirationTime = DateTime.Parse("2050-12-31 23:59:59")
  86. });
  87. if (!string.IsNullOrEmpty(result.ETag))
  88. {
  89. if (result.ETag.Length == 32)
  90. {
  91. if (File.Exists(localFile))
  92. {
  93. File.Delete(localFile);
  94. }
  95. }
  96. }
  97. if (File.Exists(FileFullName))
  98. {
  99. File.Delete(FileFullName);
  100. }
  101. }
  102. }
  103. }