CopySourceStruct.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. namespace COSXML.Model.Tag
  8. {
  9. public sealed class CopySourceStruct
  10. {
  11. /// <summary>
  12. /// cos 服务的appid
  13. /// </summary>
  14. public string appid;
  15. /// <summary>
  16. /// 存储桶名称
  17. /// </summary>
  18. public string bucket;
  19. /// <summary>
  20. /// Bucket所属地域
  21. /// </summary>
  22. public string region;
  23. /// <summary>
  24. /// 对象键
  25. /// </summary>
  26. public string key;
  27. /// <summary>
  28. /// 对象的版本ID
  29. /// </summary>
  30. public string versionId;
  31. /// <summary>
  32. /// copy source with versionId
  33. /// </summary>
  34. /// <param name="appid"></param>
  35. /// <param name="bucket"></param>
  36. /// <param name="region"></param>
  37. /// <param name="key"></param>
  38. /// <param name="versionId"></param>
  39. public CopySourceStruct(string appid, string bucket, string region, string key, string versionId)
  40. {
  41. this.appid = appid;
  42. this.bucket = bucket;
  43. this.region = region;
  44. this.key = key;
  45. this.versionId = versionId;
  46. }
  47. /// <summary>
  48. /// copy source
  49. /// </summary>
  50. /// <param name="appid"></param>
  51. /// <param name="bucket"></param>
  52. /// <param name="region"></param>
  53. /// <param name="key"></param>
  54. public CopySourceStruct(string appid, string bucket, string region, string key)
  55. : this(appid, bucket, region, key, null)
  56. {
  57. }
  58. /// <summary>
  59. /// check parameter
  60. /// </summary>
  61. public void CheckParameters()
  62. {
  63. if (bucket == null)
  64. {
  65. throw new CosClientException((int)CosClientError.InvalidArgument, "copy source bucket = null");
  66. }
  67. if (key == null)
  68. {
  69. throw new CosClientException((int)CosClientError.InvalidArgument, "copy source cosPath = null");
  70. }
  71. // if (appid == null)
  72. // {
  73. // throw new CosClientException((int)CosClientError.INVALID_ARGUMENT, "copy source appid = null");
  74. // }
  75. if (region == null)
  76. {
  77. throw new CosClientException((int)CosClientError.InvalidArgument, "copy source region = null");
  78. }
  79. }
  80. /// <summary>
  81. /// get source with urlEncode
  82. /// </summary>
  83. /// <returns></returns>
  84. public string GetCopySouce()
  85. {
  86. if (!key.StartsWith("/"))
  87. {
  88. key = "/" + key;
  89. }
  90. StringBuilder copySource = new StringBuilder();
  91. copySource.Append(bucket);
  92. if (!String.IsNullOrEmpty(appid) && !bucket.EndsWith("-" + appid))
  93. {
  94. copySource.Append("-")
  95. .Append(appid);
  96. }
  97. copySource.Append(".").Append("cos").Append(".")
  98. .Append(region).Append(".")
  99. .Append("myqcloud.com")
  100. .Append(URLEncodeUtils.EncodePathOfURL(key));
  101. if (versionId != null)
  102. {
  103. copySource.Append("?versionId=").Append(versionId);
  104. }
  105. return copySource.ToString();
  106. }
  107. }
  108. }