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