OssHelper.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Controls.Primitives;
  12. using System.Windows.Threading;
  13. using Aliyun.OSS;
  14. using Aliyun.OSS.Common;
  15. using SAGA.MBI;
  16. using SAGA.Models;
  17. namespace SAGA.GplotManage
  18. {
  19. //public class OssHelper
  20. //{
  21. // static OssClient client = new OssClient(Config.Endpoint, Config.AccessKeyId, Config.AccessKeySecret);
  22. // private static string prefixKey = "test/files/revit/GplotData/Prj10000000";
  23. // const string bucketName = "prod-data";
  24. // public static List<string> ListObjects(string bucketName)
  25. // {
  26. // List<string> fileNames = new List<string>();
  27. // Regex reg = new Regex($"{prefixKey}/(.+\\.json)");
  28. // try
  29. // {
  30. // var result = client.ListObjects(bucketName);
  31. // // var getObjectRequest = new GetObjectRequest(bucketName, $"{prefixKey}/SpaceDatas-20180730143438-new.json");
  32. // bool exist = client.DoesObjectExist(bucketName, $"{prefixKey}/SpaceDatas-20180730143438-new.json");
  33. // foreach (var summary in result.ObjectSummaries)
  34. // {
  35. // System.Diagnostics.Debug.WriteLine(summary.Key);
  36. // var match = reg.Match(summary.Key);
  37. // if (match.Success)
  38. // {
  39. // fileNames.Add(match.Groups[1].Value);
  40. // }
  41. // }
  42. // }
  43. // catch (OssException ex)
  44. // {
  45. // Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
  46. // ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
  47. // }
  48. // catch (Exception ex)
  49. // {
  50. // Console.WriteLine("Failed with error info: {0}", ex.Message);
  51. // }
  52. // return fileNames;
  53. // }
  54. // /// <summary>
  55. // /// 根据前缀列出
  56. // /// </summary>
  57. // /// <param name="bucketName"></param>
  58. // /// <param name="prefix"></param>
  59. // /// <returns></returns>
  60. // public static List<ServerFileInfo> ListObjects(string bucketName, string prefix)
  61. // {
  62. // List<ServerFileInfo> fileNames = new List<ServerFileInfo>();
  63. // Regex reg = new Regex($"{prefixKey}/(.+\\.json)");
  64. // try
  65. // {
  66. // var listObjectsRequest = new ListObjectsRequest(bucketName)
  67. // {
  68. // Prefix = prefix
  69. // };
  70. // var result = client.ListObjects(listObjectsRequest);
  71. // foreach (var summary in result.ObjectSummaries)
  72. // {
  73. // System.Diagnostics.Debug.WriteLine(summary.Key);
  74. // var match = reg.Match(summary.Key);
  75. // if (match.Success)
  76. // {
  77. // fileNames.Add(new ServerFileInfo()
  78. // {
  79. // FileName = match.Groups[1].Value,
  80. // FileKey = summary.Key
  81. // });
  82. // }
  83. // }
  84. // }
  85. // catch (OssException ex)
  86. // {
  87. // Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
  88. // ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
  89. // }
  90. // catch (Exception ex)
  91. // {
  92. // Console.WriteLine("Failed with error info: {0}", ex.Message);
  93. // }
  94. // return fileNames;
  95. // }
  96. // /// <summary>
  97. // /// 获取拓扑数据的时间戳
  98. // /// </summary>
  99. // /// <typeparam name="T"></typeparam>
  100. // /// <param name="tableName"></param>
  101. // /// <returns></returns>
  102. // public static ServerFileInfo GetTimestamp<T>(string tableName = "")
  103. // {
  104. // var file = GetTableNamePath<T>(tableName);
  105. // return file;
  106. // }
  107. // /// <summary>
  108. // /// 根据类型或者数据名称获取数据路径
  109. // /// </summary>
  110. // /// <typeparam name="T"></typeparam>
  111. // /// <param name="tableName"></param>
  112. // /// <returns></returns>
  113. // public static ServerFileInfo GetTableNamePath<T>(string tableName = "")
  114. // {
  115. // var name = tableName == "" ? GetTableName(typeof(T)) : tableName;
  116. // return GetFileNamePath(name);
  117. // }
  118. // private static string GetTableName(Type type)
  119. // {
  120. // return type.GetAttribute<TableNameAttribute>()?.Name;
  121. // }
  122. // /// <summary>
  123. // /// 根据名称查找文件
  124. // /// </summary>
  125. // /// <param name="tableName"></param>
  126. // /// <returns></returns>
  127. // public static ServerFileInfo GetFileNamePath(string tableName)
  128. // {
  129. // var file = ListObjects(bucketName, prefixKey).FirstOrDefault(f => f.FileName?.IndexOf(tableName) > -1);
  130. // return file;
  131. // }
  132. // public static void GetObjectProgress(string fileName,string key)
  133. // {
  134. // var filePath= Path.Combine(Models.StaticData.DbDirectory,fileName);
  135. // try
  136. // {
  137. // var getObjectRequest = new GetObjectRequest(bucketName, key);
  138. // // getObjectRequest.StreamTransferProgress += StreamProgressCallback;
  139. // var ossObject = client.GetObject(getObjectRequest);
  140. // using (var stream = ossObject.Content)
  141. // {
  142. // var buffer = new byte[1024 * 1024];
  143. // var bytesTotal = 0;
  144. // var bytesRead = 0;
  145. // using (var fs = File.Open(filePath, FileMode.OpenOrCreate))
  146. // {
  147. // while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
  148. // {
  149. // bytesTotal += bytesRead;
  150. // // Process read data
  151. // // TODO
  152. // fs.Write(buffer, 0, bytesRead);
  153. // }
  154. // }
  155. // }
  156. // Debug.WriteLine("Get object:{0} succeeded", key);
  157. // }
  158. // catch (OssException ex)
  159. // {
  160. // Debug.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
  161. // ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
  162. // }
  163. // catch (Exception ex)
  164. // {
  165. // Debug.WriteLine("Failed with error info: {0}", ex.Message);
  166. // }
  167. // }
  168. // public static void PutObject(string filePath)
  169. // {
  170. // string fileName = Path.GetFileName(filePath);
  171. // string key = $"{prefixKey}/{fileName}";
  172. // try
  173. // {
  174. // using (var fs = File.Open(filePath, FileMode.Open))
  175. // {
  176. // var putObjectRequest = new PutObjectRequest(bucketName, key, fs);
  177. // client.PutObject(putObjectRequest);
  178. // }
  179. // Console.WriteLine("Put object:{0} succeeded", key);
  180. // }
  181. // catch (OssException ex)
  182. // {
  183. // Console.WriteLine(@"Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
  184. // ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
  185. // }
  186. // catch (Exception ex)
  187. // {
  188. // Console.WriteLine($@"Failed with error info: { ex.Message}");
  189. // }
  190. // }
  191. // public static void PutObjectFromDir(string dirPath, Action<string> callback)
  192. // {
  193. // var files = Directory.GetFiles(dirPath);
  194. // foreach (var file in files)
  195. // {
  196. // PutObject(file);
  197. // callback?.Invoke(file);
  198. // }
  199. // }
  200. //}
  201. }