-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3_downloader.go
106 lines (86 loc) · 2.31 KB
/
s3_downloader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sync"
"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/s3"
)
var bucket = flag.String("bucket", "bucket-name", "the S3 bucket")
var accessKeyID = flag.String("access-key-id", "xxxxxx", "the S3 access key id")
var secretAccessKey = flag.String("secret-access-key", "xxxxxx", "the S3 secret access key")
var region = flag.String("region", "us-west-1", "the S3 secret access key")
var concurrency = flag.Int("concurrency", 10, "the number of downloads at the same time")
func main() {
flag.Parse()
err := downloadAll(*bucket, *region, *accessKeyID, *secretAccessKey, *concurrency)
if err != nil {
log.Fatal(err)
}
}
func downloadAll(bucketName, regionName, accessKeyID, secretAccessKey string, concurrency int) error {
auth, err := aws.GetAuth(accessKeyID, secretAccessKey)
if err != nil {
return err
}
region, ok := aws.Regions[regionName]
if !ok {
return errors.New("Invalid region")
}
client := s3.New(auth, region)
bucket := client.Bucket(bucketName)
log.Println("Getting bucket contents")
content, err := bucket.GetBucketContents()
if err != nil {
return err
}
tempDir, err := ioutil.TempDir(".", "s3_backup_")
if err != nil {
return err
}
keys := make(chan string, concurrency)
wg := &sync.WaitGroup{}
log.Printf("Starting download with concurrency of: %d", concurrency)
for i := 0; i < concurrency; i++ {
go downloadFileFromS3(*bucket, keys, tempDir, wg)
}
for key, _ := range *content {
wg.Add(1)
log.Printf("Enqueuing: %s", key)
keys <- key
}
wg.Wait()
return nil
}
func downloadFileFromS3(bucket s3.Bucket, keys chan string, tempDir string, wg *sync.WaitGroup) {
for {
key := <-keys
newDir := fmt.Sprintf("%s/%s", tempDir, filepath.Dir(key))
os.MkdirAll(newDir, 0755)
log.Printf("Downloading: %s", key)
reader, err := bucket.GetReader(key)
if err != nil {
log.Printf("Failed to download: %s, %s", key, err)
wg.Done()
continue
}
data, err := ioutil.ReadAll(reader)
if err != nil {
log.Printf("Failed to download: %s, %s", key, err)
wg.Done()
continue
}
err = ioutil.WriteFile(fmt.Sprintf("%s/%s", tempDir, key), data, 0644)
if err != nil {
log.Printf("Failed to download: %s, %s", key, err)
wg.Done()
continue
}
wg.Done()
}
}