ObjectRequest.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using COSXML.CosException;
  5. using COSXML.Common;
  6. using COSXML.Utils;
  7. using COSXML.Network;
  8. namespace COSXML.Model.Object
  9. {
  10. public class ObjectRequest : CosRequest
  11. {
  12. /// <summary>
  13. /// 存储桶名称(Bucket)
  14. /// <see href="https://cloud.tencent.com/document/product/436/7751"/>
  15. /// </summary>
  16. protected string bucket;
  17. /// <summary>
  18. /// Bucket 所在的地域
  19. /// </summary>
  20. protected string region;
  21. /// <summary>
  22. /// 对象键
  23. /// </summary>
  24. protected string key;
  25. public ObjectRequest(string bucket, string key)
  26. {
  27. this.bucket = bucket;
  28. this.key = key;
  29. if (!String.IsNullOrEmpty(key) && !key.StartsWith("/"))
  30. {
  31. this.path = "/" + key;
  32. }
  33. else
  34. {
  35. this.path = key;
  36. }
  37. }
  38. /// <summary>
  39. /// Object 所属的 Bucket
  40. /// </summary>
  41. public string Bucket
  42. {
  43. get
  44. {
  45. return this.bucket;
  46. }
  47. set { this.bucket = value; }
  48. }
  49. /// <summary>
  50. /// Bucket 所在的地域
  51. /// </summary>
  52. public string Region
  53. {
  54. get
  55. {
  56. return this.region;
  57. }
  58. set { this.region = value; }
  59. }
  60. /// <summary>
  61. /// object 名称,对象键
  62. /// </summary>
  63. public string Key
  64. {
  65. get
  66. {
  67. return this.key;
  68. }
  69. set { this.key = value; }
  70. }
  71. public override Network.RequestBody GetRequestBody()
  72. {
  73. return null;
  74. }
  75. public override string GetHost()
  76. {
  77. StringBuilder hostBuilder = new StringBuilder();
  78. if (!String.IsNullOrEmpty(serviceConfig.host))
  79. {
  80. hostBuilder.Append(serviceConfig.host);
  81. }
  82. else
  83. {
  84. hostBuilder.Append(bucket);
  85. if (!String.IsNullOrEmpty(appid) && !bucket.EndsWith("-" + appid))
  86. {
  87. hostBuilder.Append("-")
  88. .Append(appid);
  89. }
  90. if (serviceConfig.endpointSuffix != null)
  91. {
  92. hostBuilder.Append(".")
  93. .Append(serviceConfig.endpointSuffix);
  94. }
  95. else
  96. {
  97. hostBuilder.Append(".cos.")
  98. .Append(region)
  99. .Append(".myqcloud.com");
  100. }
  101. }
  102. return hostBuilder.ToString();
  103. }
  104. public override void CheckParameters()
  105. {
  106. if (requestUrlWithSign != null)
  107. {
  108. return;
  109. }
  110. //if (appid == null)
  111. //{
  112. // throw new CosClientException((int)CosClientError.INVALID_ARGUMENT, "appid is null");
  113. //}
  114. if (bucket == null || bucket.Length < 1)
  115. {
  116. throw new CosClientException((int)CosClientError.InvalidArgument, "bucket is null");
  117. }
  118. // if (region == null || region.Length < 1)
  119. // {
  120. // throw new CosClientException((int)CosClientError.INVALID_ARGUMENT, "region is null");
  121. // }
  122. if (path == null || path.Length < 1)
  123. {
  124. throw new CosClientException((int)CosClientError.InvalidArgument, "cosPath(null or empty)is invalid");
  125. }
  126. }
  127. protected virtual void InternalUpdateQueryParameters()
  128. {
  129. }
  130. protected virtual void InteranlUpdateHeaders()
  131. {
  132. }
  133. public override Dictionary<string, string> GetRequestParamters()
  134. {
  135. InternalUpdateQueryParameters();
  136. return base.GetRequestParamters();
  137. }
  138. public override Dictionary<string, string> GetRequestHeaders()
  139. {
  140. InteranlUpdateHeaders();
  141. return base.GetRequestHeaders();
  142. }
  143. /// <summary>
  144. /// cos 服务端加密
  145. /// </summary>
  146. public void SetCosServerSideEncryption()
  147. {
  148. SetRequestHeader("x-cos-server-side-encryption", "AES256");
  149. }
  150. /// <summary>
  151. /// SSEC 自定义密钥
  152. /// </summary>
  153. /// <param name="customerKey"></param>
  154. public void SetCosServerSideEncryptionWithCustomerKey(string customerKey)
  155. {
  156. if (customerKey != null)
  157. {
  158. SetRequestHeader("x-cos-server-side-encryption-customer-algorithm", "AES256");
  159. SetRequestHeader("x-cos-server-side-encryption-customer-key", DigestUtils.GetBase64(customerKey, Encoding.UTF8));
  160. SetRequestHeader("x-cos-server-side-encryption-customer-key-MD5", DigestUtils.GetMd5ToBase64(customerKey, Encoding.UTF8));
  161. }
  162. }
  163. public void SetCosServerSideEncryptionWithKMS(string customerKeyID, string json)
  164. {
  165. SetRequestHeader("x-cos-server-side-encryption", "cos/kms");
  166. if (customerKeyID != null)
  167. {
  168. SetRequestHeader("x-cos-server-side-encryption-cos-kms-key-id", customerKeyID);
  169. }
  170. if (json != null)
  171. {
  172. SetRequestHeader("x-cos-server-side-encryption-context", DigestUtils.GetBase64(json, Encoding.UTF8));
  173. }
  174. }
  175. }
  176. }