forked from danielgtaylor/apisprout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
82 lines (65 loc) · 1.47 KB
/
s3.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
)
func loadConfigFromS3WithSession(bucket string) (err error) {
sess, err := session.NewSession()
if err != nil {
return
}
s3Svc := s3.New(sess)
return loadConfigFromS3(s3Svc, bucket)
}
func loadConfigFromS3(s3Svc s3iface.S3API, bucket string) (err error) {
if !strings.HasPrefix(bucket, "s3://") {
return errors.New("invalid bucket name, missing prefix")
}
s3Uri, err := url.Parse(bucket)
if err != nil {
return
}
s3Objects, err := s3Svc.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(s3Uri.Host),
Prefix: aws.String(s3Uri.Path),
})
if err != nil {
return
}
for _, content := range s3Objects.Contents {
fmt.Printf("Getting file %s from bucket...", *content.Key)
if strings.HasSuffix(*content.Key, "/") {
continue
}
err = os.MkdirAll(filepath.Dir(*content.Key), 0755)
if err != nil {
return
}
s3ObjectData, err := s3Svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(s3Uri.Host),
Key: content.Key,
})
if err != nil {
return err
}
s3FileBody, err := ioutil.ReadAll(s3ObjectData.Body)
if err != nil {
return err
}
err = ioutil.WriteFile(*content.Key, s3FileBody, 0644)
if err != nil {
return err
}
fmt.Println(" Done")
}
return
}