79 lines
1.7 KiB
Go
79 lines
1.7 KiB
Go
package main
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
"sync"
|
||
|
|
|
||
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
||
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||
|
|
)
|
||
|
|
|
||
|
|
func handleBucketContents(ctx context.Context, client *s3.Client, s3c CloudS3, backupDir string) error {
|
||
|
|
items, err := listBucket(ctx, client, s3c)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
fmt.Printf("Bucket tartalom: %s\n", s3c.Bucket)
|
||
|
|
if s3c.Folder != nil {
|
||
|
|
fmt.Printf("Prefix: %s\n", *s3c.Folder)
|
||
|
|
}
|
||
|
|
fmt.Printf("%s\n", strings.Repeat("-", 60))
|
||
|
|
|
||
|
|
rootFolder := filepath.Join(".", backupDir, s3c.Name)
|
||
|
|
|
||
|
|
var itemsToDownload []S3Item
|
||
|
|
for _, item := range items {
|
||
|
|
if !shouldSkipDownload(rootFolder, item) {
|
||
|
|
itemsToDownload = append(itemsToDownload, item)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fmt.Printf("Összes fájl: %d, Letöltendő: %d\n", len(items), len(itemsToDownload))
|
||
|
|
|
||
|
|
sem := make(chan struct{}, 10)
|
||
|
|
var wg sync.WaitGroup
|
||
|
|
|
||
|
|
for _, item := range itemsToDownload {
|
||
|
|
item := item
|
||
|
|
wg.Add(1)
|
||
|
|
sem <- struct{}{}
|
||
|
|
go func() {
|
||
|
|
defer wg.Done()
|
||
|
|
defer func() { <-sem }()
|
||
|
|
|
||
|
|
content, err := downloadFile(ctx, client, s3c, item)
|
||
|
|
if err != nil {
|
||
|
|
fmt.Printf("✗ %s - %v\n", item.Key, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if err := createFileIfNotExistOrDiffer(rootFolder, item, content); err != nil {
|
||
|
|
fmt.Printf("✗ %s - %v\n", item.Key, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
fmt.Printf("✓ %s\n", item.Key)
|
||
|
|
}()
|
||
|
|
}
|
||
|
|
|
||
|
|
wg.Wait()
|
||
|
|
fmt.Println("Kész!")
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func downloadFile(ctx context.Context, client *s3.Client, s3c CloudS3, item S3Item) ([]byte, error) {
|
||
|
|
out, err := client.GetObject(ctx, &s3.GetObjectInput{
|
||
|
|
Bucket: aws.String(s3c.Bucket),
|
||
|
|
Key: aws.String(item.Key),
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
defer out.Body.Close()
|
||
|
|
|
||
|
|
return io.ReadAll(out.Body)
|
||
|
|
}
|