CopyObjectRequest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using COSXML.Common;
  5. using COSXML.CosException;
  6. using COSXML.Model.Tag;
  7. using COSXML.Utils;
  8. namespace COSXML.Model.Object
  9. {
  10. /// <summary>
  11. /// 实现将一个文件从源路径复制到目标路径
  12. /// <see href="https://cloud.tencent.com/document/product/436/10881"/>
  13. /// </summary>
  14. public sealed class CopyObjectRequest : ObjectRequest
  15. {
  16. /// <summary>
  17. /// 拷贝的对象源
  18. /// <see href="Model.Tag.CopySourceStruct"/>
  19. /// </summary>
  20. private CopySourceStruct copySourceStruct;
  21. public CopyObjectRequest(string bucket, string key)
  22. : base(bucket, key)
  23. {
  24. this.method = CosRequestMethod.PUT;
  25. }
  26. /// <summary>
  27. /// 设置复制的对象源
  28. /// <see href="Model.Tag.CopySourceStruct"/>
  29. /// </summary>
  30. /// <param name="copySource"></param>
  31. public void SetCopySource(CopySourceStruct copySource)
  32. {
  33. this.copySourceStruct = copySource;
  34. }
  35. /// <summary>
  36. /// 是否拷贝源文件的元数据,枚举值:Copy, Replaced,默认值 Copy。
  37. /// 假如标记为 Copy,则拷贝源文件的元数据;
  38. /// 假如标记为 Replaced,则按本次请求的 Header 信息修改元数据。
  39. /// 当目标路径和源路径一致,即用户试图修改元数据时,则标记必须为 Replaced
  40. /// </summary>
  41. /// <param name="metaDataDirective"></param>
  42. public void SetCopyMetaDataDirective(CosMetaDataDirective metaDataDirective)
  43. {
  44. SetRequestHeader(CosRequestHeaderKey.X_COS_METADATA_DIRECTIVE, EnumUtils.GetValue(metaDataDirective));
  45. }
  46. /// <summary>
  47. /// 设置 Object 的存储级别,枚举值:STANDARD,STANDARD_IA。默认值:STANDARD
  48. /// <see href="Common.CosStorageClass"/>
  49. /// </summary>
  50. /// <param name="cosStorageClass"></param>
  51. public void SetCosStorageClass(CosStorageClass cosStorageClass)
  52. {
  53. SetCosStorageClass(EnumUtils.GetValue(cosStorageClass));
  54. }
  55. /// <summary>
  56. /// 设置 Object 的存储级别
  57. /// <see href="Common.CosStorageClass"/>
  58. /// </summary>
  59. /// <param name="cosStorageClass"></param>
  60. public void SetCosStorageClass(string cosStorageClass)
  61. {
  62. SetRequestHeader(CosRequestHeaderKey.X_COS_STORAGE_CLASS, cosStorageClass);
  63. }
  64. /// <summary>
  65. /// 定义 Object 的 ACL 属性。有效值:private,public-read-write,public-read;默认值:private
  66. /// <see href="Common.CosACL"/>
  67. /// </summary>
  68. /// <param name="cosACL"></param>
  69. public void SetCosACL(string cosACL)
  70. {
  71. if (cosACL != null)
  72. {
  73. SetRequestHeader(CosRequestHeaderKey.X_COS_ACL, cosACL);
  74. }
  75. }
  76. /// <summary>
  77. /// 定义 Object 的 ACL 属性。有效值:private,public-read-write,public-read;默认值:private
  78. /// <see href="Common.CosACL"/>
  79. /// </summary>
  80. /// <param name="cosACL"></param>
  81. public void SetCosACL(CosACL cosACL)
  82. {
  83. SetCosACL(EnumUtils.GetValue(cosACL));
  84. }
  85. /// <summary>
  86. /// 赋予被授权者读的权限
  87. /// <see href="Model.Tag.GrantAccount"/>
  88. /// </summary>
  89. /// <param name="grantAccount"></param>
  90. public void SetXCosGrantRead(GrantAccount grantAccount)
  91. {
  92. if (grantAccount != null)
  93. {
  94. SetRequestHeader(CosRequestHeaderKey.X_COS_GRANT_READ, grantAccount.GetGrantAccounts());
  95. }
  96. }
  97. /// <summary>
  98. /// 赋予被授权者所有的权限
  99. /// <see href="Model.Tag.GrantAccount"/>
  100. /// </summary>
  101. /// <param name="grantAccount"></param>
  102. public void SetXCosReadWrite(GrantAccount grantAccount)
  103. {
  104. if (grantAccount != null)
  105. {
  106. SetRequestHeader(CosRequestHeaderKey.X_COS_GRANT_FULL_CONTROL, grantAccount.GetGrantAccounts());
  107. }
  108. }
  109. public override void CheckParameters()
  110. {
  111. if (copySourceStruct == null)
  112. {
  113. throw new CosClientException((int)CosClientError.InvalidArgument, "copy source is null");
  114. }
  115. else
  116. {
  117. copySourceStruct.CheckParameters();
  118. }
  119. base.CheckParameters();
  120. }
  121. protected override void InteranlUpdateHeaders()
  122. {
  123. try
  124. {
  125. this.headers.Add(CosRequestHeaderKey.X_COS_COPY_SOURCE, copySourceStruct.GetCopySouce());
  126. }
  127. catch (ArgumentException)
  128. {
  129. this.headers[CosRequestHeaderKey.X_COS_COPY_SOURCE] = copySourceStruct.GetCopySouce();
  130. }
  131. }
  132. }
  133. }