Skip to content

Commit

Permalink
fix: putBucketLifecycle add ColdArchive and DeepColdArchive
Browse files Browse the repository at this point in the history
  • Loading branch information
csg01123119 committed Oct 16, 2023
1 parent 5063347 commit fbd6972
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions lib/common/bucket/putBucketLifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion test/config.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
34 changes: 16 additions & 18 deletions test/node/bucket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
37 changes: 31 additions & 6 deletions test/node/multiversion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('test/multiversion.test.js', () => {
expiration: {
expiredObjectDeleteMarker: 'true'
},
NoncurrentVersionExpiration: {
noncurrentVersionExpiration: {
noncurrentDays: 1
}
}
Expand All @@ -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');
});
});
Expand Down

0 comments on commit fbd6972

Please sign in to comment.