-
Notifications
You must be signed in to change notification settings - Fork 143
/
s3.go
198 lines (175 loc) · 5.46 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2021 The PipeCD 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 (
"bytes"
"context"
"errors"
"fmt"
"io"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"go.uber.org/zap"
"github.com/pipe-cd/pipecd/pkg/filestore"
)
type Store struct {
client *s3.Client
bucket string
profile string
credentialsFile string
roleARN string
tokenFile string
logger *zap.Logger
}
type Option func(*Store)
func WithLogger(logger *zap.Logger) Option {
return func(s *Store) {
s.logger = logger.Named("s3")
}
}
func WithCredentialsFile(path, profile string) Option {
return func(s *Store) {
s.profile = profile
s.credentialsFile = path
}
}
func WithTokenFile(roleARN, path string) Option {
return func(s *Store) {
s.roleARN = roleARN
s.tokenFile = path
}
}
func NewStore(ctx context.Context, region, bucket string, opts ...Option) (*Store, error) {
if region == "" {
return nil, fmt.Errorf("region is required field")
}
if bucket == "" {
return nil, fmt.Errorf("bucket is required field")
}
s := &Store{
bucket: bucket,
logger: zap.NewNop(),
}
for _, opt := range opts {
opt(s)
}
optFns := []func(*config.LoadOptions) error{config.WithRegion(region)}
if s.credentialsFile != "" {
optFns = append(optFns, config.WithSharedCredentialsFiles([]string{s.credentialsFile}))
}
if s.profile != "" {
optFns = append(optFns, config.WithSharedConfigProfile(s.profile))
}
if s.tokenFile != "" && s.roleARN != "" {
optFns = append(optFns, config.WithWebIdentityRoleCredentialOptions(func(v *stscreds.WebIdentityRoleOptions) {
v.RoleARN = s.roleARN
v.TokenRetriever = stscreds.IdentityTokenFile(s.tokenFile)
}))
}
// When you initialize an aws.Config instance using config.LoadDefaultConfig, the SDK uses its default credential chain to find AWS credentials.
// This default credential chain looks for credentials in the following order:
//
// 1. Environment variables.
// 1. Static Credentials (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN)
// 2. Web Identity Token (AWS_WEB_IDENTITY_TOKEN_FILE)
// 2. Shared configuration files.
// 1. SDK defaults to credentials file under .aws folder that is placed in the home folder on your computer.
// 2. SDK defaults to config file under .aws folder that is placed in the home folder on your computer.
// 3. If your application uses an ECS task definition or RunTask API operation, IAM role for tasks.
// 4. If your application is running on an Amazon EC2 instance, IAM role for Amazon EC2.
// ref: https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/#specifying-credentials
cfg, err := config.LoadDefaultConfig(ctx, optFns...)
if err != nil {
return nil, fmt.Errorf("failed to load config to create s3 client: %w", err)
}
s.client = s3.NewFromConfig(cfg)
return s, nil
}
func (s *Store) GetReader(ctx context.Context, path string) (io.ReadCloser, error) {
input := &s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
}
out, err := s.client.GetObject(ctx, input)
if err != nil {
var nfe *types.NoSuchKey
if errors.As(err, &nfe) {
return nil, filestore.ErrNotFound
}
return nil, err
}
return out.Body, nil
}
func (s *Store) Get(ctx context.Context, path string) ([]byte, error) {
rc, err := s.GetReader(ctx, path)
if err != nil {
return nil, err
}
defer func() {
if err := rc.Close(); err != nil {
s.logger.Error("failed to close object reader")
}
}()
return io.ReadAll(rc)
}
func (s *Store) Put(ctx context.Context, path string, content []byte) error {
input := &s3.PutObjectInput{
Body: bytes.NewReader(content),
Bucket: aws.String(s.bucket),
Key: aws.String(path),
}
_, err := s.client.PutObject(ctx, input)
if err != nil {
return err
}
return nil
}
func (s *Store) Delete(ctx context.Context, path string) error {
input := &s3.DeleteObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
}
_, err := s.client.DeleteObject(ctx, input)
return err
}
func (s *Store) List(ctx context.Context, prefix string) ([]filestore.ObjectAttrs, error) {
var objects []filestore.ObjectAttrs
input := &s3.ListObjectsV2Input{
Bucket: aws.String(s.bucket),
Prefix: aws.String(prefix),
}
paginator := s3.NewListObjectsV2Paginator(s.client, input)
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get list objects: %w", err)
}
for _, obj := range page.Contents {
objects = append(objects, filestore.ObjectAttrs{
Path: aws.ToString(obj.Key),
Size: obj.Size,
Etag: aws.ToString(obj.ETag),
UpdatedAt: aws.ToTime(obj.LastModified).Unix(),
})
}
}
return objects, nil
}
func (s *Store) Close() error {
// aws client does not provide the way to close a connection via sdk
return nil
}