46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
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
|
||
|
|
}
|