-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.go
72 lines (59 loc) · 1.38 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
package s3
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/rai-project/aws"
"github.com/rai-project/config"
"github.com/rai-project/store"
)
type options struct {
}
type s3Client struct {
client *s3.S3
uploader *s3manager.Uploader
downloader *s3manager.Downloader
s3Opts options
opts *store.Options
}
func New(iopts ...store.Option) (store.Store, error) {
s3Opts := options{}
opts := NewOptions()
for _, o := range iopts {
o(opts)
}
var sess *session.Session
if s, ok := opts.Context.Value(sessionKey{}).(*session.Session); ok && s != nil {
sess = s.Copy()
}
if sess == nil {
var err error
sess, err = aws.NewSession()
if err != nil {
return nil, err
}
}
sess.Config.WithEndpoint(opts.BaseURL)
if config.IsVerbose || config.IsDebug {
sess.Config.WithCredentialsChainVerboseErrors(true).WithLogger(log)
}
client := s3.New(sess)
uploader := s3manager.NewUploaderWithClient(client)
downloader := s3manager.NewDownloaderWithClient(client)
return &s3Client{
client: client,
uploader: uploader,
downloader: downloader,
s3Opts: s3Opts,
opts: opts,
}, nil
}
func (s *s3Client) Options() store.Options {
return *s.opts
}
func (*s3Client) Name() string {
return "S3"
}
func (c *s3Client) Close() error {
return nil
}