/
s3.go
133 lines (115 loc) · 2.84 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package storage
import (
"io"
"strings"
"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"
)
type S3Client interface {
PutObject(*s3.PutObjectInput) (*s3.PutObjectOutput, error)
ListObjects(*s3.ListObjectsInput) (*s3.ListObjectsOutput, error)
DeleteObject(*s3.DeleteObjectInput) (*s3.DeleteObjectOutput, error)
}
type S3 struct {
Client S3Client
Key string
Secret string
Region string
Bucket string
Endpoint string
DisableSSL bool
S3ForcePathStyle bool
}
func (s *S3) K8sSecretName() string {
return "s3-storage"
}
func (s *S3) AccessData() map[string][]byte {
return map[string][]byte{
"region": []byte(s.Region),
"builder-bucket": []byte(s.Bucket),
"accesskey": []byte(s.Key),
"secretkey": []byte(s.Secret),
}
}
func (s *S3) UploadFile(path string, file io.ReadSeeker) error {
po := &s3.PutObjectInput{
Bucket: &s.Bucket,
Body: file,
Key: &path,
}
_, err := s.Client.PutObject(po)
return err
}
func (s *S3) List(path string) ([]*Object, error) {
res, err := s.s3List(path)
if err != nil {
return nil, err
}
out := []*Object{}
m := make(map[string]bool)
for _, item := range res.Contents {
name := strings.TrimPrefix(*item.Key, path)
name = strings.Split(name, "/")[0]
if _, found := m[name]; !found {
m[name] = true
out = append(out, &Object{Name: name, LastModified: *item.LastModified})
}
}
return out, nil
}
func (s *S3) Delete(path string) error {
objs, err := s.s3List(path)
if err != nil {
return err
}
for _, obj := range objs.Contents {
di := &s3.DeleteObjectInput{
Bucket: &s.Bucket,
Key: obj.Key,
}
if _, err := s.Client.DeleteObject(di); err != nil {
return err
}
}
return nil
}
func (s *S3) s3List(path string) (*s3.ListObjectsOutput, error) {
li := &s3.ListObjectsInput{
Bucket: &s.Bucket,
Prefix: aws.String(path),
}
return s.Client.ListObjects(li)
}
func (s *S3) Type() string {
return string(S3Type)
}
func (s *S3) PodEnvVars() map[string]string {
if s.Endpoint != "" {
return map[string]string{
"S3_HOST": s.Endpoint,
}
}
return make(map[string]string)
}
func newS3(conf *Config) *S3 {
st := &S3{
Key: conf.AwsKey,
Region: conf.AwsRegion,
Secret: conf.AwsSecret,
Bucket: conf.AwsBucket,
Endpoint: conf.AwsEndpoint,
DisableSSL: conf.AwsDisableSSL,
S3ForcePathStyle: conf.AwsS3ForcePathStyle,
}
awsConf := &aws.Config{
Credentials: credentials.NewStaticCredentials(st.Key, st.Secret, ""),
Region: &st.Region,
Endpoint: &st.Endpoint,
DisableSSL: &st.DisableSSL,
S3ForcePathStyle: &st.S3ForcePathStyle,
}
st.Client = s3.New(session.New(), awsConf)
return st
}