AppendObjectRequest.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using COSXML.Common;
  2. using System.IO;
  3. using COSXML.Model.Tag;
  4. using COSXML.Utils;
  5. using COSXML.CosException;
  6. using COSXML.Network;
  7. namespace COSXML.Model.Object
  8. {
  9. /// <summary>
  10. /// 简单上传对象
  11. /// <see href="https://cloud.tencent.com/document/product/436/7749"/>
  12. /// </summary>
  13. public sealed class AppendObjectRequest : ObjectRequest
  14. {
  15. private static string TAG = typeof(PutObjectRequest).FullName;
  16. /// <summary>
  17. /// 本地文件路径
  18. /// </summary>
  19. private string srcPath;
  20. /// <summary>
  21. /// 追加到对象的位置
  22. /// </summary>
  23. private long appendPosition;
  24. /// <summary>
  25. /// 读文件的起始位置
  26. /// </summary>
  27. private long fileOffset = 0;
  28. /// <summary>
  29. /// 上传data数据
  30. /// </summary>
  31. private byte[] data;
  32. /// <summary>
  33. /// 上传指定内容的长度
  34. /// </summary>
  35. private long contentLength = -1L;
  36. /// <summary>
  37. /// 上传回调
  38. /// </summary>
  39. private COSXML.Callback.OnProgressCallback progressCallback;
  40. /// <summary>
  41. /// 上传文件的指定范围到对象末尾
  42. /// </summary>
  43. /// <param name="bucket"></param>
  44. /// <param name="key"></param>
  45. /// <param name="srcPath"></param>
  46. /// <param name="appendPosition">追加到对象的位置,首次上传应为0</param>
  47. /// <param name="fileOffset">读文件的起始位置</param>
  48. /// <param name="needSendLength">文件指定内容长度</param>
  49. public AppendObjectRequest(string bucket, string key, string srcPath, long appendPosition, long fileOffset, long needSendLength)
  50. : this(bucket, key, srcPath, appendPosition)
  51. {
  52. this.fileOffset = fileOffset < 0 ? 0 : fileOffset;
  53. this.contentLength = needSendLength < 0 ? -1L : needSendLength;
  54. }
  55. /// <summary>
  56. /// 上传文件到对象末尾
  57. /// </summary>
  58. /// <param name="bucket"></param>
  59. /// <param name="key"></param>
  60. /// <param name="srcPath"></param>
  61. /// <param name="appendPosition">追加到对象的位置,首次上传为0</param>
  62. public AppendObjectRequest(string bucket, string key, string srcPath, long appendPosition)
  63. : base(bucket, key)
  64. {
  65. this.method = CosRequestMethod.POST;
  66. this.srcPath = srcPath;
  67. this.appendPosition = appendPosition < 0 ? 0 : appendPosition;
  68. this.queryParameters.Add("append", null);
  69. this.queryParameters.Add("position", appendPosition.ToString());
  70. }
  71. /// <summary>
  72. /// 上传data数据到对象末尾
  73. /// </summary>
  74. /// <param name="bucket"></param>
  75. /// <param name="key"></param>
  76. /// <param name="data"></param>
  77. /// <param name="appendPosition"></param>
  78. public AppendObjectRequest(string bucket, string key, byte[] data, long appendPosition)
  79. : base(bucket, key)
  80. {
  81. this.appendPosition = appendPosition < 0 ? 0 : appendPosition;
  82. this.queryParameters.Add("append", null);
  83. this.queryParameters.Add("position", appendPosition.ToString());
  84. this.method = CosRequestMethod.POST;
  85. this.data = data;
  86. }
  87. /// <summary>
  88. /// 上传回调
  89. /// </summary>
  90. /// <param name="progressCallback"></param>
  91. public void SetCosProgressCallback(COSXML.Callback.OnProgressCallback progressCallback)
  92. {
  93. this.progressCallback = progressCallback;
  94. }
  95. /// <summary>
  96. /// 设置 Object 的存储级别
  97. /// <see href="Common.CosStorageClass"/>
  98. /// </summary>
  99. /// <param name="cosStorageClass"></param>
  100. public void SetCosStorageClass(string cosStorageClass)
  101. {
  102. SetRequestHeader(CosRequestHeaderKey.X_COS_STORAGE_CLASS, cosStorageClass);
  103. }
  104. public override void CheckParameters()
  105. {
  106. if (srcPath == null && data == null)
  107. {
  108. throw new CosClientException((int)(CosClientError.InvalidArgument), "data source = null");
  109. }
  110. if (srcPath != null)
  111. {
  112. if (!File.Exists(srcPath))
  113. {
  114. throw new CosClientException((int)(CosClientError.InvalidArgument), "file not exist");
  115. }
  116. }
  117. if(fileOffset > 0) {
  118. }
  119. base.CheckParameters();
  120. }
  121. public override Network.RequestBody GetRequestBody()
  122. {
  123. RequestBody body = null;
  124. if (srcPath != null)
  125. {
  126. FileInfo fileInfo = new FileInfo(srcPath);
  127. if (contentLength == -1 || contentLength + fileOffset > fileInfo.Length)
  128. {
  129. contentLength = fileInfo.Length - fileOffset;
  130. }
  131. body = new FileRequestBody(srcPath, fileOffset, contentLength);
  132. body.ProgressCallback = progressCallback;
  133. }
  134. else if (data != null)
  135. {
  136. body = new ByteRequestBody(data);
  137. body.ProgressCallback = progressCallback;
  138. }
  139. return body;
  140. }
  141. /// <summary>
  142. /// 定义 Object 的 acl 属性。有效值:private,public-read-write,public-read;默认值:private
  143. /// <see href="Common.CosACL"/>
  144. /// </summary>
  145. /// <param name="cosACL"></param>
  146. public void SetCosACL(string cosACL)
  147. {
  148. if (cosACL != null)
  149. {
  150. SetRequestHeader(CosRequestHeaderKey.X_COS_ACL, cosACL);
  151. }
  152. }
  153. /// <summary>
  154. /// 最大上传速度,单位是 bit/s
  155. /// </summary>
  156. /// <param name="rate"></param>
  157. public void LimitTraffic(long rate)
  158. {
  159. SetRequestHeader(CosRequestHeaderKey.X_COS_TRAFFIC_LIMIT, rate.ToString());
  160. }
  161. /// <summary>
  162. /// 定义 Object 的 acl 属性。有效值:private,public-read-write,public-read;默认值:private
  163. /// <see href="Common.CosACL"/>
  164. /// </summary>
  165. /// <param name="cosACL"></param>
  166. public void SetCosACL(CosACL cosACL)
  167. {
  168. SetCosACL(EnumUtils.GetValue(cosACL));
  169. }
  170. /// <summary>
  171. /// 赋予被授权者读的权限
  172. /// <see href="Model.Tag.GrantAccount"/>
  173. /// </summary>
  174. /// <param name="grantAccount"></param>
  175. public void SetXCosGrantRead(GrantAccount grantAccount)
  176. {
  177. if (grantAccount != null)
  178. {
  179. SetRequestHeader(CosRequestHeaderKey.X_COS_GRANT_READ, grantAccount.GetGrantAccounts());
  180. }
  181. }
  182. /// <summary>
  183. /// 赋予被授权者所有的权限
  184. /// <see href="Model.Tag.GrantAccount"/>
  185. /// </summary>
  186. /// <param name="grantAccount"></param>
  187. public void SetXCosReadWrite(GrantAccount grantAccount)
  188. {
  189. if (grantAccount != null)
  190. {
  191. SetRequestHeader(CosRequestHeaderKey.X_COS_GRANT_FULL_CONTROL, grantAccount.GetGrantAccounts());
  192. }
  193. }
  194. }
  195. }