From fbd69720c22e67217b7ce9fd4b40d4a94df62627 Mon Sep 17 00:00:00 2001 From: csg01123119 Date: Mon, 16 Oct 2023 10:21:37 +0800 Subject: [PATCH] fix: putBucketLifecycle add ColdArchive and DeepColdArchive --- README.md | 4 +-- lib/common/bucket/putBucketLifecycle.js | 15 ++++++++++ test/config.js | 2 +- test/node/bucket.test.js | 34 +++++++++++------------ test/node/multiversion.test.js | 37 +++++++++++++++++++++---- 5 files changed, 65 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index c6e09a521..10a2a6aad 100644 --- a/README.md +++ b/README.md @@ -957,12 +957,12 @@ parameters: - [createdBeforeDate] {String} expire date, e.g.: `2022-10-11T00:00:00.000Z` `createdBeforeDate` and `days` must have one. - [transition] {Object} Specifies the time when an object is converted to the IA or archive storage class during a valid life cycle. - - storageClass {String} Specifies the storage class that objects that conform to the rule are converted into. allow values: `IA` or `Archive` + - storageClass {String} Specifies the storage class that objects that conform to the rule are converted into. allow values: `IA` or `Archive` or `ColdArchive` or `DeepColdArchive` - [days] {Number|String} expire after the `days` - [createdBeforeDate] {String} expire date, e.g.: `2022-10-11T00:00:00.000Z` `createdBeforeDate` and `days` must have one. - [noncurrentVersionTransition] {Object} Specifies the time when an object is converted to the IA or archive storage class during a valid life cycle. - - storageClass {String} Specifies the storage class that history objects that conform to the rule are converted into. allow values: `IA` or `Archive` + - storageClass {String} Specifies the storage class that history objects that conform to the rule are converted into. allow values: `IA` or `Archive` or `ColdArchive` or `DeepColdArchive` - noncurrentDays {String} expire after the `noncurrentDays` `expiration`、 `abortMultipartUpload`、 `transition`、 `noncurrentVersionTransition` must have one. - [noncurrentVersionExpiration] {Object} specifies the expiration attribute of the lifecycle rules for the history object. diff --git a/lib/common/bucket/putBucketLifecycle.js b/lib/common/bucket/putBucketLifecycle.js index 8eece21e1..2235e432e 100644 --- a/lib/common/bucket/putBucketLifecycle.js +++ b/lib/common/bucket/putBucketLifecycle.js @@ -73,6 +73,15 @@ function checkDaysAndDate(obj, key) { } } +function checkNoncurrentDays(obj, key) { + const { noncurrentDays } = obj; + if (!noncurrentDays) { + throw new Error(`${key} must includes noncurrentDays`); + } else if (noncurrentDays && !/^[1-9][0-9]*$/.test(noncurrentDays)) { + throw new Error('noncurrentDays must be a positive integer'); + } +} + function handleCheckTag(tag) { if (!isArray(tag) && !isObject(tag)) { throw new Error('tag must be Object or Array'); @@ -118,6 +127,12 @@ function checkRule(rule) { ); } + if (rule.noncurrentVersionTransition) { + if (!['IA', 'Archive', 'ColdArchive', 'DeepColdArchive'].includes(rule.noncurrentVersionTransition.storageClass)) + throw new Error('noncurrentVersionTransition StorageClass must be IA、Archive、ColdArchive、DeepColdArchive'); + checkNoncurrentDays(rule.noncurrentVersionTransition, 'NoncurrentVersionTransition'); + } + if (rule.tag) { if (rule.abortMultipartUpload) { throw new Error('Tag cannot be used with abortMultipartUpload'); diff --git a/test/config.js b/test/config.js index a58ca7611..80101e450 100644 --- a/test/config.js +++ b/test/config.js @@ -1,7 +1,7 @@ const { env } = process; const config = module.exports; -const USWEST = 'oss-us-west-1'; // ONCI=true Using the region of Silicon Valley in the United States would be faster +const USWEST = 'oss-ap-southeast-1'; // ONCI=true Faster using oss-ap-outsoutheast-1 config.oss = { accessKeyId: env.ALI_SDK_OSS_ID, diff --git a/test/node/bucket.test.js b/test/node/bucket.test.js index ab0d89222..539269d72 100644 --- a/test/node/bucket.test.js +++ b/test/node/bucket.test.js @@ -873,25 +873,23 @@ describe('test/bucket.test.js', () => { } ]); assert.equal(putresult3.res.status, 200); - // oss-us-west-1 not support DeepColdArchive - if (!config.endpoint) { - const putresult4 = await store.putBucketLifecycle(bucket, [ - { - id: 'transition4', - prefix: 'logs/', - status: 'Enabled', - transition: { - days: 20, - storageClass: 'DeepColdArchive' - }, - tag: { - key: 'test4', - value: '123' - } + // Regions that need to support DeepColdArchive + const putresult4 = await store.putBucketLifecycle(bucket, [ + { + id: 'transition4', + prefix: 'logs/', + status: 'Enabled', + transition: { + days: 20, + storageClass: 'DeepColdArchive' + }, + tag: { + key: 'test4', + value: '123' } - ]); - assert.equal(putresult4.res.status, 200); - } + } + ]); + assert.equal(putresult4.res.status, 200); }); it('should put the lifecycle with expiration and Tag', async () => { diff --git a/test/node/multiversion.test.js b/test/node/multiversion.test.js index c115e3354..008d38eda 100644 --- a/test/node/multiversion.test.js +++ b/test/node/multiversion.test.js @@ -145,7 +145,7 @@ describe('test/multiversion.test.js', () => { expiration: { expiredObjectDeleteMarker: 'true' }, - NoncurrentVersionExpiration: { + noncurrentVersionExpiration: { noncurrentDays: 1 } } @@ -155,25 +155,50 @@ describe('test/multiversion.test.js', () => { assert.strictEqual(rules[0].expiration.expiredObjectDeleteMarker, 'true'); }); - it('should putBucketLifecycle with noncurrentVersionTransition', async () => { - const putresult1 = await store.putBucketLifecycle(bucket, [ + it.only('should putBucketLifecycle with noncurrentVersionTransition', async () => { + const putresult = await store.putBucketLifecycle(bucket, [ { - id: 'expiration1', - prefix: 'logs/', + prefix: 'log/', status: 'Enabled', noncurrentVersionTransition: { noncurrentDays: '10', storageClass: 'IA' } + }, + { + prefix: 'logs/', + status: 'Enabled', + noncurrentVersionTransition: { + noncurrentDays: '10', + storageClass: 'Archive' + } + }, + { + prefix: 'logss/', + status: 'Enabled', + noncurrentVersionTransition: { + noncurrentDays: '10', + storageClass: 'ColdArchive' + } + }, + { + prefix: 'logsss/', + status: 'Enabled', + noncurrentVersionTransition: { + noncurrentDays: '10', + storageClass: 'DeepColdArchive' + } } ]); - assert.equal(putresult1.res.status, 200); + assert.equal(putresult.res.status, 200); + const { rules } = await store.getBucketLifecycle(bucket); const [ { noncurrentVersionTransition: { noncurrentDays, storageClass } } ] = rules; + assert(noncurrentDays === '10' && storageClass === 'IA'); }); });