local clean
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
)
|
||||
|
||||
func deleteOldFiles(ctx context.Context, client *s3.Client, s3c CloudS3, days int) (int, error) {
|
||||
items, err := listBucket(ctx, client, s3c)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
cutoff := time.Now().UTC().Add(-time.Duration(days) * 24 * time.Hour)
|
||||
deleted := 0
|
||||
|
||||
for _, item := range items {
|
||||
if item.IsFolder || item.LastModified == nil {
|
||||
continue
|
||||
}
|
||||
lm, err := time.Parse(time.RFC3339, *item.LastModified)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if lm.Before(cutoff) {
|
||||
_, err := client.DeleteObject(ctx, &s3.DeleteObjectInput{
|
||||
Bucket: aws.String(s3c.Bucket),
|
||||
Key: aws.String(item.Key),
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("Hiba a törléskor: %s - %v\n", item.Key, err)
|
||||
continue
|
||||
}
|
||||
fmt.Printf("🗑 Törölve: %s (%s)\n", item.Key, *item.LastModified)
|
||||
deleted++
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("Törlés kész: %d fájl törölve (%d napnál régebbiek)\n", deleted, days)
|
||||
return deleted, nil
|
||||
}
|
||||
Reference in New Issue
Block a user