This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
forked from coreos/etcd-operator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.go
150 lines (129 loc) · 3.51 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2016 The kube-etcd-etcd-operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package s3
import (
"io"
"path"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
const (
v1 = "v1/"
)
// S3 is a helper layer to wrap complex S3 logic.
type S3 struct {
bucket string
prefix string
client *s3.S3
}
// New returns a S3 translator from default shared config.
// Please refer to http://docs.aws.amazon.com/sdk-for-go/api/aws/session/
// for setting up credentials and configuration.
func New(bucket, prefix string) (*S3, error) {
return NewFromSessionOpt(bucket, prefix, session.Options{
// Setting this is equal to the AWS_SDK_LOAD_CONFIG environment variable was set.
// We want to save the work to set AWS_SDK_LOAD_CONFIG=1 outside.
SharedConfigState: session.SharedConfigEnable,
})
}
func NewFromSessionOpt(bucket, prefix string, so session.Options) (*S3, error) {
sess, err := session.NewSessionWithOptions(so)
if err != nil {
return nil, err
}
cli := s3.New(sess)
return &S3{
bucket: bucket,
prefix: prefix,
client: cli,
}, nil
}
func NewFromClient(bucket, prefix string, cli *s3.S3) *S3 {
return &S3{
bucket: bucket,
prefix: prefix,
client: cli,
}
}
func (s *S3) Put(key string, rs io.ReadSeeker) error {
_, err := s.client.PutObject(&s3.PutObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path.Join(v1, s.prefix, key)),
Body: rs,
})
return err
}
func (s *S3) Get(key string) (io.ReadCloser, error) {
resp, err := s.client.GetObject(&s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path.Join(v1, s.prefix, key)),
})
if err != nil {
return nil, err
}
return resp.Body, nil
}
func (s *S3) Delete(key string) error {
_, err := s.client.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path.Join(v1, s.prefix, key)),
})
return err
}
func (s *S3) List() ([]string, error) {
_, l, err := s.list(s.prefix)
return l, err
}
func (s *S3) list(prefix string) (int64, []string, error) {
resp, err := s.client.ListObjects(&s3.ListObjectsInput{
Bucket: aws.String(s.bucket),
// s3 doesn't have dir. It only recognizes prefix.
// Thus "a/b" has prefix "a/"
Prefix: aws.String(path.Join(v1, prefix) + "/"),
})
if err != nil {
return -1, nil, err
}
keys := []string{}
var size int64
for _, key := range resp.Contents {
k := (*key.Key)[len(*resp.Prefix):]
keys = append(keys, k)
size += *key.Size
}
return size, keys, nil
}
func (s *S3) TotalSize() (int64, error) {
size, _, err := s.list(s.prefix)
return size, err
}
func (s *S3) CopyPrefix(from string) error {
_, keys, err := s.list(from)
if err != nil {
return err
}
for _, key := range keys {
req := &s3.CopyObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path.Join(v1, s.prefix, key)),
CopySource: aws.String(path.Join(s.bucket, v1, from, key)),
}
_, err := s.client.CopyObject(req)
if err != nil {
return err
}
}
return nil
}