mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(s3): apply lifecycle expiration rules (#5158)
A bucket's lifecycle configuration was stored and returned but never evaluated. A background sweep now applies its expiration actions: - Expiration (Days, Date) deletes the current version of a matching object, or adds a delete marker in a versioning-enabled bucket. Day counts run from the last-modified time and round up to the next midnight UTC, as on S3. - ExpiredObjectDeleteMarker, and a Days or Date expiration once the marker is old enough, remove a delete marker with no other versions. - NoncurrentVersionExpiration (NoncurrentDays, NewerNoncurrentVersions) permanently deletes noncurrent versions, counting from when the next newer version was written. - AbortIncompleteMultipartUpload (DaysAfterInitiation) aborts uploads under a prefix-only filter. Filters are Prefix, Tag, ObjectSizeGreaterThan, ObjectSizeLessThan, an And of them, or the older rule-level Prefix. Disabled rules are skipped, versions under Object Lock retention or a legal hold are never deleted, and each bucket is swept in its owning account. Lifecycle deletions are notified as s3:LifecycleExpiration:* rather than s3:ObjectRemoved:*, with S3 itself as the requester, and their EventBridge reason is "Lifecycle Expiration". A rule S3 would have refused on put, such as Days 0, a size bound that is not a number or a Tag without a Value, is skipped as a whole, so a bad value never widens what a rule deletes. A body that is not one well-formed LifecycleConfiguration document has no rules. Each key is planned from one pass over its account's objects, then read again by its known store keys and applied under the bucket monitor. Lifecycle configuration changes and tagging now change under that monitor too, and a multipart upload is aborted only while its bucket and the rules are still the ones the sweep started on. Versions written in the same millisecond keep the order they were written in, for ListObjectVersions as well as for lifecycle. PutObject, GetObject and HeadObject on a current version that an Expiration rule matches return x-amz-expiration with the expiry date and the URL-encoded rule id. The sweep runs every 60 seconds by default, configured with floci.services.s3.lifecycle-sweep-enabled and floci.services.s3.lifecycle-sweep-interval-seconds. Transition actions are still not applied. Closes #5137
This commit is contained in:
1 parent
fea7d7b0b1
commit
cbd2f61dc4
18 files changed
+1846
-33
No files matched your search
@@ -7,6 +7,7 @@ import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.BucketAlreadyOwnedByYouException;
|
||||
import software.amazon.awssdk.services.s3.model.BucketLifecycleConfiguration;
|
||||
@@ -14,12 +15,20 @@ import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
|
||||
import software.amazon.awssdk.services.s3.model.DeleteBucketRequest;
|
||||
import software.amazon.awssdk.services.s3.model.ExpirationStatus;
|
||||
import software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationResponse;
|
||||
import software.amazon.awssdk.services.s3.model.HeadObjectResponse;
|
||||
import software.amazon.awssdk.services.s3.model.LifecycleExpiration;
|
||||
import software.amazon.awssdk.services.s3.model.LifecycleRule;
|
||||
import software.amazon.awssdk.services.s3.model.LifecycleRuleFilter;
|
||||
import software.amazon.awssdk.services.s3.model.PutBucketLifecycleConfigurationResponse;
|
||||
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
|
||||
import software.amazon.awssdk.services.s3.model.TransitionDefaultMinimumObjectSize;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
@@ -40,6 +49,9 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
class S3LifecycleTest {
|
||||
|
||||
private static final String BUCKET = "compat-lifecycle-bucket";
|
||||
private static final DateTimeFormatter HTTP_DATE = DateTimeFormatter
|
||||
.ofPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US)
|
||||
.withZone(ZoneOffset.UTC);
|
||||
|
||||
private static S3Client s3;
|
||||
|
||||
@@ -117,4 +129,25 @@ class S3LifecycleTest {
|
||||
.as("GET must default to ALL_STORAGE_CLASSES_128_K when PUT omits the header")
|
||||
.isEqualTo(TransitionDefaultMinimumObjectSize.ALL_STORAGE_CLASSES_128_K);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
@DisplayName("#5137 objects under an Expiration rule report expiry-date and rule-id")
|
||||
void objectsUnderAnExpirationRuleReportTheirExpiry() {
|
||||
// The configuration from @Order(2): Days 365 on every key, rule id expire-everything.
|
||||
String key = "expiring.txt";
|
||||
try {
|
||||
PutObjectResponse put = s3.putObject(req -> req.bucket(BUCKET).key(key), RequestBody.fromString("hi"));
|
||||
HeadObjectResponse head = s3.headObject(req -> req.bucket(BUCKET).key(key));
|
||||
|
||||
// Creation time plus 365 days, rounded up to the next midnight UTC.
|
||||
Instant expiry = head.lastModified().plus(365, ChronoUnit.DAYS)
|
||||
.truncatedTo(ChronoUnit.DAYS).plus(1, ChronoUnit.DAYS);
|
||||
String expected = "expiry-date=\"" + HTTP_DATE.format(expiry) + "\", rule-id=\"expire-everything\"";
|
||||
assertThat(head.expiration()).isEqualTo(expected);
|
||||
assertThat(put.expiration()).isEqualTo(expected);
|
||||
} finally {
|
||||
s3.deleteObject(req -> req.bucket(BUCKET).key(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user