putBucketLifecycle.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* eslint-disable no-use-before-define */
  2. const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
  3. const { isArray } = require('../utils/isArray');
  4. const { deepCopy } = require('../utils/deepCopy');
  5. const { isObject } = require('../utils/isObject');
  6. const { obj2xml } = require('../utils/obj2xml');
  7. const { checkObjectTag } = require('../utils/checkObjectTag');
  8. const { getStrBytesCount } = require('../utils/getStrBytesCount');
  9. const proto = exports;
  10. proto.putBucketLifecycle = async function putBucketLifecycle(name, rules, options) {
  11. _checkBucketName(name);
  12. if (!isArray(rules)) {
  13. throw new Error('rules must be Array');
  14. }
  15. const params = this._bucketRequestParams('PUT', name, 'lifecycle', options);
  16. const Rule = [];
  17. const paramXMLObj = {
  18. LifecycleConfiguration: {
  19. Rule
  20. }
  21. };
  22. rules.forEach(_ => {
  23. defaultDaysAndDate2Expiration(_); // todo delete, 兼容旧版本
  24. checkRule(_);
  25. if (_.id) {
  26. _.ID = _.id;
  27. delete _.id;
  28. }
  29. Rule.push(_);
  30. });
  31. const paramXML = obj2xml(paramXMLObj, {
  32. headers: true,
  33. firstUpperCase: true
  34. });
  35. params.content = paramXML;
  36. params.mime = 'xml';
  37. params.successStatuses = [200];
  38. const result = await this.request(params);
  39. return {
  40. res: result.res
  41. };
  42. };
  43. // todo delete, 兼容旧版本
  44. function defaultDaysAndDate2Expiration(obj) {
  45. if (obj.days) {
  46. obj.expiration = {
  47. days: obj.days
  48. };
  49. }
  50. if (obj.date) {
  51. obj.expiration = {
  52. createdBeforeDate: obj.date
  53. };
  54. }
  55. }
  56. function checkDaysAndDate(obj, key) {
  57. const { days, createdBeforeDate } = obj;
  58. if (!days && !createdBeforeDate) {
  59. throw new Error(`${key} must includes days or createdBeforeDate`);
  60. } else if (days && !/^[1-9][0-9]*$/.test(days)) {
  61. throw new Error('days must be a positive integer');
  62. } else if (createdBeforeDate && !/\d{4}-\d{2}-\d{2}T00:00:00.000Z/.test(createdBeforeDate)) {
  63. throw new Error('createdBeforeDate must be date and conform to iso8601 format');
  64. }
  65. }
  66. function handleCheckTag(tag) {
  67. if (!isArray(tag) && !isObject(tag)) {
  68. throw new Error('tag must be Object or Array');
  69. }
  70. tag = isObject(tag) ? [tag] : tag;
  71. const tagObj = {};
  72. const tagClone = deepCopy(tag);
  73. tagClone.forEach(v => {
  74. tagObj[v.key] = v.value;
  75. });
  76. checkObjectTag(tagObj);
  77. }
  78. function checkRule(rule) {
  79. if (rule.id && getStrBytesCount(rule.id) > 255) throw new Error('ID is composed of 255 bytes at most');
  80. if (rule.prefix === undefined) throw new Error('Rule must includes prefix');
  81. if (!['Enabled', 'Disabled'].includes(rule.status)) throw new Error('Status must be Enabled or Disabled');
  82. if (rule.transition) {
  83. if (!['IA', 'Archive'].includes(rule.transition.storageClass))
  84. throw new Error('StorageClass must be IA or Archive');
  85. checkDaysAndDate(rule.transition, 'Transition');
  86. }
  87. if (rule.expiration) {
  88. if (!rule.expiration.expiredObjectDeleteMarker) {
  89. checkDaysAndDate(rule.expiration, 'Expiration');
  90. } else if (rule.expiration.days || rule.expiration.createdBeforeDate) {
  91. throw new Error('expiredObjectDeleteMarker cannot be used with days or createdBeforeDate');
  92. }
  93. }
  94. if (rule.abortMultipartUpload) {
  95. checkDaysAndDate(rule.abortMultipartUpload, 'AbortMultipartUpload');
  96. }
  97. if (!rule.expiration && !rule.abortMultipartUpload && !rule.transition && !rule.noncurrentVersionTransition) {
  98. throw new Error(
  99. 'Rule must includes expiration or abortMultipartUpload or transition or noncurrentVersionTransition'
  100. );
  101. }
  102. if (rule.tag) {
  103. if (rule.abortMultipartUpload) {
  104. throw new Error('Tag cannot be used with abortMultipartUpload');
  105. }
  106. handleCheckTag(rule.tag);
  107. }
  108. }