AliyunOss.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using Aliyun.OSS;
  2. using DaJiaoYan.Models;
  3. using DaJiaoYan.Utils;
  4. using System;
  5. using System.IO;
  6. using System.Text.RegularExpressions;
  7. namespace DaJiaoYan.Services
  8. {
  9. public static class AliyunOss
  10. {
  11. private static AliyunOssClient client = null;
  12. /// <summary>
  13. /// 客户端锁
  14. /// </summary>
  15. private readonly static object ClientLock = new object();
  16. private static AliyunOssClient Client
  17. {
  18. get
  19. {
  20. if (client == null)
  21. {
  22. lock (ClientLock)
  23. {
  24. client = new AliyunOssClient();
  25. }
  26. }
  27. return client;
  28. }
  29. }
  30. /// <summary>
  31. /// 上传
  32. /// </summary>
  33. /// <param name="bucketName"></param>
  34. /// <param name="objectName"></param>
  35. /// <param name="requestContent"></param>
  36. /// <returns></returns>
  37. public static bool SimpleUpload(string objectName, MemoryStream requestContent)
  38. {
  39. return Client.SimpleUpload(objectName, requestContent);
  40. }
  41. /// <summary>
  42. /// 上传
  43. /// </summary>
  44. /// <param name="bucketName"></param>
  45. /// <param name="objectName"></param>
  46. /// <param name="localFilename"></param>
  47. /// <returns></returns>
  48. public static bool SimpleUpload(string objectName, string localFilename)
  49. {
  50. return Client.SimpleUpload(objectName, localFilename);
  51. }
  52. }
  53. public class AliyunOssClient
  54. {
  55. public delegate void DeleWriteLog(string txt);
  56. public static DeleWriteLog WriteLog;
  57. private static OssClient client = null;
  58. private static AliyunAccessModel accessModel = null;
  59. /// <summary>
  60. /// 客户端锁
  61. /// </summary>
  62. private readonly static object ClientLock = new object();
  63. /// <summary>
  64. /// 授权有效期临界值
  65. /// </summary>
  66. private const int EXP_THRESHOLD = 60 * 2 * 1000;
  67. private readonly string token = null;
  68. public AliyunOssClient(string token = null)
  69. {
  70. this.token = token;
  71. }
  72. private AliyunAccessModel Access
  73. {
  74. get
  75. {
  76. lock (ClientLock)
  77. {
  78. if (accessModel == null || accessModel.Expiration - EXP_THRESHOLD < Functions.GetTimeStamp())
  79. {
  80. //lock (accessModel)
  81. //{
  82. accessModel = Api.GetAliyunAccess(token).Result;
  83. if (accessModel != null)
  84. {
  85. accessModel.Expiration = Functions.GetTimeStamp() + accessModel.Expiration * 1000;
  86. accessModel.EndPoint = Regex.Replace(accessModel.EndPoint, @"-internal", "", RegexOptions.IgnoreCase);
  87. }
  88. //}
  89. }
  90. }
  91. return accessModel;
  92. }
  93. }
  94. public bool AccessAvailable
  95. {
  96. get
  97. {
  98. return !string.IsNullOrEmpty(Access?.EndPoint) && !string.IsNullOrEmpty(Access?.AccessKeyId) && !string.IsNullOrEmpty(Access?.AccessKeySecret);
  99. }
  100. }
  101. private OssClient Client
  102. {
  103. get
  104. {
  105. if (client == null && AccessAvailable)
  106. {
  107. client = new OssClient(Access?.EndPoint, Access?.AccessKeyId, Access?.AccessKeySecret);
  108. }
  109. return client;
  110. }
  111. }
  112. /// <summary>
  113. /// 上传
  114. /// </summary>
  115. /// <param name="bucketName"></param>
  116. /// <param name="objectName"></param>
  117. /// <param name="requestContent"></param>
  118. /// <returns></returns>
  119. public bool SimpleUpload(string objectName, Stream requestContent)
  120. {
  121. //// yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
  122. //var endpoint = "yourEndpoint";
  123. //// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
  124. //var accessKeyId = "yourAccessKeyId";
  125. //var accessKeySecret = "yourAccessKeySecret";
  126. //// 填写Bucket名称。
  127. //var bucketName = "examplebucket";
  128. //// 填写Object完整路径。Object完整路径中不能包含Bucket名称。
  129. //var objectName = "exampleobject.txt";
  130. //// 填写字符串。
  131. //var objectContent = "More than just cloud.";
  132. // 创建OssClient实例。
  133. //var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  134. bool res = false;
  135. try
  136. {
  137. //byte[] binaryData = Encoding.ASCII.GetBytes(objectContent);
  138. //MemoryStream requestContent = new MemoryStream(binaryData);
  139. // 上传文件。
  140. Client?.PutObject(Access?.BucketName, objectName, requestContent);
  141. res = true;
  142. }
  143. catch (Exception ex)
  144. {
  145. WriteLog?.Invoke($"Put object failed, {ex.Message}");
  146. WriteLog?.Invoke(ex.StackTrace);
  147. }
  148. return res;
  149. }
  150. /// <summary>
  151. /// 上传
  152. /// </summary>
  153. /// <param name="bucketName"></param>
  154. /// <param name="objectName"></param>
  155. /// <param name="localFilename"></param>
  156. /// <returns></returns>
  157. public bool SimpleUpload(string objectName, string localFilename)
  158. {
  159. bool res = false;
  160. //PutObjectResult r;
  161. try
  162. {
  163. //byte[] binaryData = Encoding.ASCII.GetBytes(objectContent);
  164. //MemoryStream requestContent = new MemoryStream(binaryData);
  165. for (int i = 0; i < 5; i++)
  166. {
  167. using (PutObjectResult r = Client?.PutObject(Access?.BucketName, objectName, localFilename))
  168. {
  169. //r = t.
  170. if (r?.HttpStatusCode == System.Net.HttpStatusCode.OK)
  171. {
  172. res = true;
  173. break;
  174. }
  175. }
  176. }
  177. // 上传文件。
  178. }
  179. catch (Exception ex)
  180. {
  181. WriteLog?.Invoke($"Put object failed, {ex.Message}");
  182. WriteLog?.Invoke(ex.StackTrace);
  183. }
  184. return res;
  185. }
  186. }
  187. }