CopyObjectRequest.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) Alibaba Cloud Computing
  3. * All rights reserved.
  4. *
  5. * 版权所有 (C)阿里云计算有限公司
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using Aliyun.OSS.Util;
  10. namespace Aliyun.OSS
  11. {
  12. #pragma warning disable 618, 3005
  13. /// <summary>
  14. /// 指定拷贝Object的请求参数
  15. /// </summary>
  16. public class CopyObjectRequest
  17. {
  18. private readonly IList<string> _matchingETagConstraints = new List<string>();
  19. private readonly IList<string> _nonmatchingETagConstraints = new List<string>();
  20. /// <summary>
  21. /// 获取或者设置源Object所在的Bucket的名称。
  22. /// </summary>
  23. public string SourceBucketName { get; set; }
  24. /// <summary>
  25. /// 获取或者设置源Object的Key。
  26. /// </summary>
  27. public string SourceKey { get; set; }
  28. /// <summary>
  29. /// 获取或者设置目标Object所在的Bucket的名称。
  30. /// </summary>
  31. public string DestinationBucketName { get; set; }
  32. /// <summary>
  33. /// 获取或者设置目标Object的Key。
  34. /// </summary>
  35. public string DestinationKey { get; set; }
  36. /// <summary>
  37. /// 获取或者设置目标Object的Metadata信息。
  38. /// </summary>
  39. public ObjectMetadata NewObjectMetadata { get; set; }
  40. /// <summary>
  41. /// 如果源Object的ETAG值和用户提供的ETAG相等,则执行拷贝操作;否则返回412 HTTP错误码(预处理失败)。
  42. /// </summary>
  43. public IList<string> MatchingETagConstraints
  44. {
  45. get { return _matchingETagConstraints; }
  46. }
  47. /// <summary>
  48. /// 如果源Object的ETAG值和用户提供的ETAG不相等,则执行拷贝操作;否则返回412 HTTP错误码(预处理失败)。
  49. /// </summary>
  50. public IList<string> NonmatchingETagConstraints
  51. {
  52. get { return _nonmatchingETagConstraints; }
  53. }
  54. /// <summary>
  55. /// 如果传入参数中的时间等于或者晚于文件实际修改时间,则正常传输文件,并返回200 OK;
  56. /// 否则返回412 precondition failed错误
  57. /// </summary>
  58. public DateTime? UnmodifiedSinceConstraint { get; set; }
  59. /// <summary>
  60. /// 如果源Object自从用户指定的时间以后被修改过,则执行拷贝操作;
  61. /// 否则返回412 HTTP错误码(预处理失败)。
  62. /// </summary>
  63. public DateTime? ModifiedSinceConstraint { get; set; }
  64. /// <summary>
  65. /// 构造一个新的<see cref="CopyObjectRequest" /> 实例
  66. /// </summary>
  67. /// <param name="sourceBucketName">需要拷贝的<see cref="OssObject" />所在的Bucket</param>
  68. /// <param name="sourceKey">需要拷贝的<see cref="OssObject" />名称</param>
  69. /// <param name="destinationBucketName">要拷贝到的目的<see cref="OssObject" />所在的Bucket</param>
  70. /// <param name="destinationKey">要拷贝到的目的<see cref="OssObject" />的名称</param>
  71. public CopyObjectRequest(string sourceBucketName, string sourceKey,
  72. string destinationBucketName, string destinationKey)
  73. {
  74. OssUtils.CheckBucketName(destinationBucketName);
  75. OssUtils.CheckObjectKey(destinationKey);
  76. SourceBucketName = sourceBucketName;
  77. SourceKey = sourceKey;
  78. DestinationBucketName = destinationBucketName;
  79. DestinationKey = destinationKey;
  80. }
  81. internal void Populate(IDictionary<string, string> headers)
  82. {
  83. var copyHeaderValue = OssUtils.BuildCopyObjectSource(SourceBucketName, SourceKey);
  84. headers.Add(OssHeaders.CopyObjectSource, copyHeaderValue);
  85. if (ModifiedSinceConstraint != null)
  86. {
  87. headers.Add(OssHeaders.CopySourceIfModifedSince,
  88. DateUtils.FormatRfc822Date(ModifiedSinceConstraint.Value));
  89. }
  90. if (UnmodifiedSinceConstraint != null)
  91. {
  92. headers.Add(OssHeaders.CopySourceIfUnmodifiedSince,
  93. DateUtils.FormatRfc822Date(UnmodifiedSinceConstraint.Value));
  94. }
  95. if (_matchingETagConstraints.Count > 0)
  96. {
  97. headers.Add(OssHeaders.CopySourceIfMatch, OssUtils.JoinETag(_matchingETagConstraints));
  98. }
  99. if (_nonmatchingETagConstraints.Count > 0)
  100. {
  101. headers.Add(OssHeaders.CopySourceIfNoneMatch,
  102. OssUtils.JoinETag(_nonmatchingETagConstraints));
  103. }
  104. if (NewObjectMetadata != null)
  105. {
  106. headers.Add(OssHeaders.CopyObjectMetaDataDirective, "REPLACE");
  107. NewObjectMetadata.Populate(headers);
  108. }
  109. // Remove Content-Length header, ObjectMeta#Populate will create
  110. // ContentLength header, but we do not need it for the request body is empty.
  111. headers.Remove(HttpHeaders.ContentLength);
  112. }
  113. }
  114. #pragma warning restore 618, 3005
  115. }