From 0bf0144deb9423130bb13dd4d5d74d4aa9360524 Mon Sep 17 00:00:00 2001 From: nyanotech Date: Thu, 4 Apr 2024 23:18:56 -0700 Subject: [PATCH] skip getting current retention settings if update-within is 0, default that to 0 on b2, putobjectretention is free, so might as well yolo it --- main.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 632288d..e98d751 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,7 @@ var bucket = flag.String("bucket", "", "bucket name") var accessKeyId = flag.String("access-key-id", "", "aws access key") var secretAccessKey = flag.String("secret-access-key", "", "aws secret key") -var updateExpiresWithin = flag.Int("update-expires-within", 30*24*3600, "only update objects whose lock expires within this many seconds (default 30 days)") +var updateExpiresWithin = flag.Int("update-expires-within", 0, "only update objects whose lock expires within this many seconds (default 0)") var lockFor = flag.Int("lock-for", 90*24*3600, "how many seconds to renew the object lock for (default 90 days)") var wg sync.WaitGroup @@ -71,12 +71,22 @@ func main() { } func checkAndRenewObjectLock(svc *s3.Client, object string) { - retention, _ := svc.GetObjectRetention(context.TODO(), &s3.GetObjectRetentionInput{ - Bucket: bucket, - Key: &object, - }) + updateHold := false + if *updateExpiresWithin == 0 { + updateHold = true + } else { + retention, _ := svc.GetObjectRetention(context.TODO(), &s3.GetObjectRetentionInput{ + Bucket: bucket, + Key: &object, + }) + if retention == nil { + updateHold = true + } else if retention.Retention.RetainUntilDate.Before(time.Now().Add(time.Second * time.Duration(*updateExpiresWithin))) { + updateHold = true + } + } - if retention == nil || retention.Retention.RetainUntilDate.Before(time.Now().Add(time.Second * time.Duration(*updateExpiresWithin))) { + if updateHold { log.Println("Renewing object lock for object", object) _, err := svc.PutObjectRetention(context.TODO(), &s3.PutObjectRetentionInput{ Bucket: bucket,