-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths3fetcher.go
84 lines (68 loc) · 2.16 KB
/
s3fetcher.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
package main
import (
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
var _ FileFetcher = (*S3configs)(nil)
// S3configs holds basic config data required by S3fetcher
type S3configs struct {
BucketName string
CredentialsFile string
Region string
s3svc *s3.S3
}
// NewS3Fetcher is a S3 backed implementation of the FileFetcher interface.
// it does the setup of the S3 service session state required to implement FileFetcher interface
func NewS3Fetcher(cfg S3configs) FileFetcher {
awsCfg := aws.NewConfig().WithRegion(cfg.Region)
if cfg.CredentialsFile != "" {
log.Printf("using AWS credentials from %s", cfg.CredentialsFile)
awsCfg = awsCfg.WithCredentials(credentials.NewSharedCredentials(cfg.CredentialsFile, "default"))
} else {
log.Print("no AWS credentials file specified, using the default credentials chain")
}
svc := s3.New(session.New(awsCfg))
cfg.s3svc = svc
return cfg
}
// GetFile from S3 bucket identified by key
func (s3cfg S3configs) GetFile(key string) (*FetchedFile, error) {
res, err := s3cfg.s3svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(s3cfg.BucketName),
Key: aws.String(key),
})
if err != nil {
return nil, err
}
ret := &FetchedFile{Payload: res.Body, Etag: *res.ETag, Key: key, BucketName: s3cfg.BucketName}
return ret, nil
}
// ListDir returns the contents of our preconfigured bucket that start with path
func (s3cfg S3configs) ListDir(path string) ([]ListDirEntry, error) {
params := &s3.ListObjectsInput{
Bucket: aws.String(s3cfg.BucketName),
Prefix: aws.String(path),
}
var items s3KeyList
if err := s3cfg.s3svc.ListObjectsPages(params, items.processPage); err != nil {
return nil, err
}
return items.keyList, nil
}
type s3KeyList struct {
keyList []ListDirEntry
}
func (list *s3KeyList) processPage(page *s3.ListObjectsOutput, more bool) bool {
for _, obj := range page.Contents {
entry := ListDirEntry{
Name: *obj.Key,
LastModified: *obj.LastModified,
SizeKb: *obj.Size / 1024,
}
list.keyList = append(list.keyList, entry)
}
return true
}