PutObjectRequest.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 PutObjectRequest : 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 fileOffset = 0L;
  24. /// <summary>
  25. /// 上传data数据
  26. /// </summary>
  27. private byte[] data;
  28. /// <summary>
  29. /// 上传指定内容的长度
  30. /// </summary>
  31. private long contentLength = -1L;
  32. /// <summary>
  33. /// 上传流
  34. /// </summary>
  35. private Stream stream;
  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. public PutObjectRequest(string bucket, string key, string srcPath)
  47. : this(bucket, key, srcPath, -1L, -1L)
  48. {
  49. }
  50. /// <summary>
  51. /// 上传文件的指定内容
  52. /// </summary>
  53. /// <param name="bucket"></param>
  54. /// <param name="key"></param>
  55. /// <param name="srcPath"></param>
  56. /// <param name="fileOffset">文件指定起始位置</param>
  57. /// <param name="needSendLength">文件指定内容长度</param>
  58. public PutObjectRequest(string bucket, string key, string srcPath, long fileOffset, long needSendLength)
  59. : base(bucket, key)
  60. {
  61. this.method = CosRequestMethod.PUT;
  62. this.srcPath = srcPath;
  63. this.fileOffset = fileOffset < 0 ? 0 : fileOffset;
  64. this.contentLength = needSendLength < 0 ? -1L : needSendLength;
  65. }
  66. /// <summary>
  67. /// 上传data数据
  68. /// </summary>
  69. /// <param name="bucket"></param>
  70. /// <param name="key"></param>
  71. /// <param name="data"></param>
  72. public PutObjectRequest(string bucket, string key, byte[] data) : base(bucket, key)
  73. {
  74. this.method = CosRequestMethod.PUT;
  75. this.data = data;
  76. }
  77. /// <summary>
  78. /// 流式上传
  79. /// </summary>
  80. /// <param name="bucket"></param>
  81. /// <param name="key"></param>
  82. /// <param name="stream"></param>
  83. public PutObjectRequest(string bucket, string key, Stream stream) : base(bucket, key)
  84. {
  85. this.stream = stream;
  86. this.method = CosRequestMethod.PUT;
  87. }
  88. /// <summary>
  89. /// 指定 offset 的流式上传
  90. /// </summary>
  91. /// <param name="bucket"></param>
  92. /// <param name="key"></param>
  93. /// <param name="stream"></param>
  94. /// <param name="offset"></param>
  95. public PutObjectRequest(string bucket, string key, Stream stream, long offset) : this(bucket, key, stream)
  96. {
  97. this.fileOffset = offset;
  98. }
  99. /// <summary>
  100. /// 指定 offset + content-length 的流式上传
  101. /// </summary>
  102. /// <param name="bucket"></param>
  103. /// <param name="key"></param>
  104. /// <param name="stream"></param>
  105. /// <param name="offset"></param>
  106. /// <param name="needSendLength"></param>
  107. public PutObjectRequest(string bucket, string key, Stream stream, long offset, long needSendLength) : this(bucket, key, stream, offset)
  108. {
  109. this.contentLength = needSendLength < 0 ? -1L : needSendLength;
  110. }
  111. /// <summary>
  112. /// 上传回调
  113. /// </summary>
  114. /// <param name="progressCallback"></param>
  115. public void SetCosProgressCallback(COSXML.Callback.OnProgressCallback progressCallback)
  116. {
  117. this.progressCallback = progressCallback;
  118. }
  119. /// <summary>
  120. /// 设置 Object 的存储级别
  121. /// <see href="Common.CosStorageClass"/>
  122. /// </summary>
  123. /// <param name="cosStorageClass"></param>
  124. public void SetCosStorageClass(string cosStorageClass)
  125. {
  126. SetRequestHeader(CosRequestHeaderKey.X_COS_STORAGE_CLASS, cosStorageClass);
  127. }
  128. public override void CheckParameters()
  129. {
  130. if (srcPath == null && data == null && stream == null)
  131. {
  132. throw new CosClientException((int)(CosClientError.InvalidArgument), "data source = null");
  133. }
  134. if (srcPath != null)
  135. {
  136. if (!File.Exists(srcPath))
  137. {
  138. throw new CosClientException((int)(CosClientError.InvalidArgument), "file not exist");
  139. }
  140. }
  141. if (stream != null)
  142. {
  143. if (stream.Length <= fileOffset ||
  144. !stream.CanRead ||
  145. !stream.CanSeek)
  146. {
  147. throw new CosClientException((int)(CosClientError.InvalidArgument), "stream invalid");
  148. }
  149. }
  150. base.CheckParameters();
  151. }
  152. public override Network.RequestBody GetRequestBody()
  153. {
  154. RequestBody body = null;
  155. if (srcPath != null)
  156. {
  157. FileInfo fileInfo = new FileInfo(srcPath);
  158. if (contentLength == -1 || contentLength + fileOffset > fileInfo.Length)
  159. {
  160. contentLength = fileInfo.Length - fileOffset;
  161. }
  162. body = new FileRequestBody(srcPath, fileOffset, contentLength);
  163. body.ProgressCallback = progressCallback;
  164. }
  165. else if (data != null)
  166. {
  167. body = new ByteRequestBody(data);
  168. body.ProgressCallback = progressCallback;
  169. }
  170. else if (stream != null)
  171. {
  172. if (fileOffset < 0)
  173. {
  174. fileOffset = 0L;
  175. }
  176. if (contentLength == -1L)
  177. {
  178. contentLength = stream.Length - fileOffset;
  179. }
  180. if (fileOffset + contentLength > stream.Length)
  181. {
  182. throw new CosException.CosClientException(
  183. (int)COSXML.Common.CosClientError.InvalidArgument,
  184. "stream offset + contentLength greater than stream.Length");
  185. }
  186. body = new StreamRequestBody(stream, fileOffset, contentLength);
  187. }
  188. return body;
  189. }
  190. /// <summary>
  191. /// 定义 Object 的 acl 属性。有效值:private,public-read-write,public-read;默认值:private
  192. /// <see href="Common.CosACL"/>
  193. /// </summary>
  194. /// <param name="cosACL"></param>
  195. public void SetCosACL(string cosACL)
  196. {
  197. if (cosACL != null)
  198. {
  199. SetRequestHeader(CosRequestHeaderKey.X_COS_ACL, cosACL);
  200. }
  201. }
  202. /// <summary>
  203. /// 最大上传速度,单位是 bit/s
  204. /// </summary>
  205. /// <param name="rate"></param>
  206. public void LimitTraffic(long rate)
  207. {
  208. SetRequestHeader(CosRequestHeaderKey.X_COS_TRAFFIC_LIMIT, rate.ToString());
  209. }
  210. /// <summary>
  211. /// 定义 Object 的 acl 属性。有效值:private,public-read-write,public-read;默认值:private
  212. /// <see href="Common.CosACL"/>
  213. /// </summary>
  214. /// <param name="cosACL"></param>
  215. public void SetCosACL(CosACL cosACL)
  216. {
  217. SetCosACL(EnumUtils.GetValue(cosACL));
  218. }
  219. /// <summary>
  220. /// 赋予被授权者读的权限
  221. /// <see href="Model.Tag.GrantAccount"/>
  222. /// </summary>
  223. /// <param name="grantAccount"></param>
  224. public void SetXCosGrantRead(GrantAccount grantAccount)
  225. {
  226. if (grantAccount != null)
  227. {
  228. SetRequestHeader(CosRequestHeaderKey.X_COS_GRANT_READ, grantAccount.GetGrantAccounts());
  229. }
  230. }
  231. /// <summary>
  232. /// 赋予被授权者所有的权限
  233. /// <see href="Model.Tag.GrantAccount"/>
  234. /// </summary>
  235. /// <param name="grantAccount"></param>
  236. public void SetXCosReadWrite(GrantAccount grantAccount)
  237. {
  238. if (grantAccount != null)
  239. {
  240. SetRequestHeader(CosRequestHeaderKey.X_COS_GRANT_FULL_CONTROL, grantAccount.GetGrantAccounts());
  241. }
  242. }
  243. }
  244. }