From 56dc194afab16c1564a90ef4973d7e2ff83a1f23 Mon Sep 17 00:00:00 2001 From: kagchi Date: Tue, 17 Oct 2023 09:46:14 +0700 Subject: [PATCH] feat: retain file after 3 days --- .env.example | 3 ++- main.go | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 6feb27f..d19c2ed 100644 --- a/.env.example +++ b/.env.example @@ -9,4 +9,5 @@ S3_SECRET= S3_USE_SSL= S3_BUCKET= -CRON_JOB=0 */2 * * * \ No newline at end of file +CRON_JOB=0 */2 * * * +FILE_PATH=Database \ No newline at end of file diff --git a/main.go b/main.go index 2edbc1a..615f5b1 100644 --- a/main.go +++ b/main.go @@ -101,7 +101,7 @@ func main() { currentTime := time.Now() year, month, day := currentTime.Date() date := fmt.Sprintf("%d-%02d-%02d", year, month, day) - fileName := fmt.Sprintf("Database/%d/%d/%d/%s (%s).zip", year, month, day, date, randHex) + fileName := fmt.Sprintf("%s/%d/%d/%d/%s (%s).zip", os.Getenv("FILE_PATH"), year, month, day, date, randHex) log.Printf("File name: %s", fileName) @@ -112,6 +112,15 @@ func main() { } log.Printf("Uploaded the file to S3") + + retainTime := currentTime.Add(3 * 24 * time.Hour) + + client.PutObjectRetention(context.Background(), os.Getenv("S3_BUCKET"), fileName, minio.PutObjectRetentionOptions{ + RetainUntilDate: &retainTime, + VersionID: fmt.Sprintf("Retain %s after 3 days", fileName), + }) + + log.Printf("%s will be retained after 3 days", fileName) }, )