ClientConfiguration.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using Aliyun.OSS;
  10. using Aliyun.OSS.Util;
  11. namespace Aliyun.OSS.Common
  12. {
  13. /// <summary>
  14. /// 表示访问阿里云服务的配置信息。
  15. /// </summary>
  16. public class ClientConfiguration : ICloneable
  17. {
  18. private const string UserAgentPrefix = "aliyun-sdk-dotnet/";
  19. private static readonly string _userAgent = GetDefaultUserAgent();
  20. // A temporary solution for undesired abortion when putting/getting large objects,
  21. // asynchronous implementions of putobject/getobject in next version can be a better
  22. // solution to solve such problems.
  23. private int _connectionTimeout = -1;
  24. private int _maxErrorRetry = 3;
  25. private int _proxyPort = -1;
  26. private bool _isCname = false;
  27. /// <summary>
  28. /// HttpWebRequest最大的并发连接数目。
  29. /// </summary>
  30. public const int ConnectionLimit = 512;
  31. private Protocol _protocol = Protocol.Http;
  32. /// <summary>
  33. /// 获取访问请求的User-Agent。
  34. /// </summary>
  35. public string UserAgent
  36. {
  37. get { return _userAgent; }
  38. }
  39. /// <summary>
  40. /// 获取或设置代理服务器的地址。
  41. /// </summary>
  42. public string ProxyHost { get; set; }
  43. /// <summary>
  44. /// 获取或设置代理服务器的端口。
  45. /// </summary>
  46. public int ProxyPort
  47. {
  48. get { return _proxyPort; }
  49. set { _proxyPort = value; }
  50. }
  51. /// <summary>
  52. /// 获取或设置用户名。
  53. /// </summary>
  54. public string ProxyUserName { get; set; }
  55. /// <summary>
  56. /// 获取或设置密码。
  57. /// </summary>
  58. public string ProxyPassword { get; set; }
  59. /// <summary>
  60. /// 获取或设置代理服务器授权用户所在的域。
  61. /// </summary>
  62. public string ProxyDomain { get; set; }
  63. /// <summary>
  64. /// 获取或设置连接超时时间,单位为毫秒。
  65. /// </summary>
  66. public int ConnectionTimeout
  67. {
  68. get { return _connectionTimeout; }
  69. set { _connectionTimeout = value; }
  70. }
  71. /// <summary>
  72. /// 获取或设置请求发生错误时最大的重试次数。
  73. /// </summary>
  74. public int MaxErrorRetry
  75. {
  76. get { return _maxErrorRetry; }
  77. set { _maxErrorRetry = value; }
  78. }
  79. /// <summary>
  80. /// 获取或设置请求OSS服务时采用的通信协议。
  81. /// </summary>
  82. public Protocol Protocol
  83. {
  84. get { return _protocol; }
  85. set { _protocol = value; }
  86. }
  87. /// <summary>
  88. /// 获取或设置请求的Endpoint是否是cname
  89. /// 如果是cname,不支持ListBuckets操作
  90. /// </summary>
  91. public bool IsCname
  92. {
  93. get
  94. {
  95. return _isCname;
  96. }
  97. set
  98. {
  99. _isCname = value;
  100. }
  101. }
  102. /// <summary>
  103. /// <para>设置自定义基准时间。</para>
  104. /// <para>
  105. /// 由于OSS的token校验是时间相关的,可能会因为终端系统时间不准导致无法访问OSS服务。
  106. /// 通过该接口设置自定义Epoch秒数,SDK计算出本机当前时间与自定义时间的差值,之后的
  107. /// 每次请求时间均加上该差值,以达到时间校验的目的。
  108. /// </para>
  109. /// </summary>
  110. /// <param name="epochTicks">自定义Epoch秒数。</param>
  111. public void SetCustomEpochTicks(long epochTicks)
  112. {
  113. var epochTime = new DateTime(1970, 1, 1);
  114. var timeSpan = DateTime.UtcNow.Subtract(epochTime);
  115. var localTicks = (long)timeSpan.TotalSeconds;
  116. TickOffset = epochTicks - localTicks;
  117. }
  118. /// <summary>
  119. /// 获取自定义基准时间与本地时间的时差值,单位为秒。
  120. /// </summary>
  121. public long TickOffset { get; internal set; }
  122. /// <summary>
  123. /// 获取该实例的拷贝。
  124. /// </summary>
  125. /// <returns>该实例的拷贝。</returns>
  126. public object Clone()
  127. {
  128. return new ClientConfiguration
  129. {
  130. ConnectionTimeout = ConnectionTimeout,
  131. MaxErrorRetry = MaxErrorRetry,
  132. ProxyDomain = ProxyDomain,
  133. ProxyHost = ProxyHost,
  134. ProxyPassword = ProxyPassword,
  135. ProxyPort = ProxyPort,
  136. ProxyUserName = ProxyUserName,
  137. TickOffset = TickOffset
  138. };
  139. }
  140. /// <summary>
  141. /// 获取User-Agent信息。
  142. /// </summary>
  143. private static string GetDefaultUserAgent()
  144. {
  145. return UserAgentPrefix +
  146. typeof(ClientConfiguration).Assembly.GetName().Version + "(" +
  147. OssUtils.DetermineOsVersion() + "/" +
  148. Environment.OSVersion.Version + "/" +
  149. OssUtils.DetermineSystemArchitecture() + ";" +
  150. Environment.Version + ")";
  151. }
  152. }
  153. }