Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARSN-218: support lifecycle noncurrent version transition #1924

Open
wants to merge 1 commit into
base: development/8.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export const supportedLifecycleRules = [
'noncurrentVersionExpiration',
'abortIncompleteMultipartUpload',
'transitions',
'noncurrentVersionTransition',
'noncurrentVersionTransitions',
];
// Maximum number of buckets to cache (bucket metadata)
export const maxCachedBuckets = process.env.METADATA_MAX_CACHED_BUCKETS ?
Expand Down
8 changes: 8 additions & 0 deletions lib/models/LifecycleRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class LifecycleRule {
if (this.transitions) {
rule.Transitions = this.transitions;
}
if (this.ncvTransitions) {
rule.NoncurrentVersionTransitions = this.ncvTransitions;
}


const filter = {};
Expand Down Expand Up @@ -133,6 +136,11 @@ class LifecycleRule {
this.transitions = transitions;
return this;
}

addNCVTransitions(nvcTransitions) {
this.ncvTransitions = nvcTransitions;
return this;
}
}

module.exports = LifecycleRule;
7 changes: 6 additions & 1 deletion lib/s3middleware/lifecycleHelpers/LifecycleDateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class LifecycleDateTime {
* @return - The normalized transition timestamp
*/
getTransitionTimestamp(
transition: { Date?: string; Days?: number },
transition: { Date?: string; Days?: number, NoncurrentDays?: number },
lastModified: string,
) {
if (transition.Date !== undefined) {
Expand All @@ -55,5 +55,10 @@ export default class LifecycleDateTime {
const timeTravel = this._transitionOneDayEarlier ? -oneDay : 0;
return lastModifiedTime + (transition.Days * oneDay) + timeTravel;
}
if (transition.NoncurrentDays !== undefined) {
const lastModifiedTime = this.getTimestamp(lastModified);
const timeTravel = this._transitionOneDayEarlier ? -oneDay : 0;
return lastModifiedTime + (transition.NoncurrentDays * oneDay) + timeTravel;
}
}
}
59 changes: 58 additions & 1 deletion lib/s3middleware/lifecycleHelpers/LifecycleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,46 @@ export default class LifecycleUtils {
});
}

/**
* Find the most relevant trantition rule for the given transitions array
* and any previously stored transition from another rule.
* @param params.noncurrentTransitions - Array of lifecycle rule noncurrent
* transitions
* @param params.lastModified - The object's last modified
* @param params.currentDate - current date
* @param params.store - object containing applicable rules
* date
* @return The most applicable transition rule
*/
getApplicableNoncurrentVersionTransition(params: {
store: any;
currentDate: Date;
noncurrentTransitions: any[];
lastModified: string;
}) {
const { noncurrentTransitions, store, lastModified, currentDate } = params;
const ncvt = noncurrentTransitions.reduce((result, ncvt) => {
const isApplicable = // Is the transition time in the past?
this._datetime.getTimestamp(currentDate) >=
this._datetime.getTransitionTimestamp(ncvt, lastModified)!;
if (!isApplicable) {
return result;
}
return this.compareTransitions({
transition1: ncvt,
transition2: result,
lastModified,
});
}, undefined);


return this.compareTransitions({
transition1: ncvt,
transition2: store.NoncurrentVersionTransition,
lastModified,
});
}

// TODO
/**
* Filter out all rules based on `Status` and `Filter` (Prefix and Tags)
Expand Down Expand Up @@ -239,14 +279,31 @@ export default class LifecycleUtils {
currentDate,
});
}
// TODO: Add support for NoncurrentVersionTransitions.

const ncvt = 'NoncurrentVersionTransitions';
const hasNoncurrentTransitions = Array.isArray(rule[ncvt]) && rule[ncvt].length > 0;
if (hasNoncurrentTransitions && this._supportedRules.includes('noncurrentVersionTransitions')) {
store.NoncurrentVersionTransition = this.getApplicableNoncurrentVersionTransition({
noncurrentTransitions: rule.NoncurrentVersionTransitions,
lastModified: metadata.LastModified,
store,
currentDate,
});
}

return store;
}, {});
// Do not transition to a location where the object is already stored.
if (applicableRules.Transition
&& applicableRules.Transition.StorageClass === metadata.StorageClass) {
applicableRules.Transition = undefined;
}

if (applicableRules.NoncurrentVersionTransition
&& applicableRules.NoncurrentVersionTransition.StorageClass === metadata.StorageClass) {
applicableRules.NoncurrentVersionTransition = undefined;
}

return applicableRules;
/* eslint-enable no-param-reassign */
}
Expand Down
Loading