-
Notifications
You must be signed in to change notification settings - Fork 12
/
s3.go
205 lines (180 loc) · 6.05 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
199
200
201
202
203
204
205
package implementations
import (
"bytes"
"context"
"errors"
"fmt"
"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"
types2 "github.com/jitsucom/bulker/bulkerlib/types"
"github.com/jitsucom/bulker/jitsubase/errorj"
"github.com/jitsucom/bulker/jitsubase/logging"
"go.uber.org/atomic"
"io"
"time"
)
// S3Config is a dto for config deserialization
type S3Config struct {
FileConfig `mapstructure:",squash" json:",inline" yaml:",inline"`
AccessKey string `mapstructure:"accessKeyId,omitempty" json:"accessKeyId,omitempty" yaml:"accessKeyId,omitempty"`
SecretKey string `mapstructure:"secretAccessKey,omitempty" json:"secretAccessKey,omitempty" yaml:"secretAccessKey,omitempty"`
Bucket string `mapstructure:"bucket,omitempty" json:"bucket,omitempty" yaml:"bucket,omitempty"`
Region string `mapstructure:"region,omitempty" json:"region,omitempty" yaml:"region,omitempty"`
Endpoint string `mapstructure:"endpoint,omitempty" json:"endpoint,omitempty" yaml:"endpoint,omitempty"`
}
// Validate returns err if invalid
func (s3c *S3Config) Validate() error {
if s3c == nil {
return errors.New("S3 config is required")
}
if s3c.AccessKey == "" {
return errors.New("S3 accessKey is required parameter")
}
if s3c.SecretKey == "" {
return errors.New("S3 secretKey is required parameter")
}
if s3c.Bucket == "" {
return errors.New("S3 bucket is required parameter")
}
if s3c.Region == "" {
return errors.New("S3 region is required parameter")
}
return nil
}
// S3 is a S3 adapter for uploading/deleting files
type S3 struct {
AbstractFileAdapter
config *S3Config
client *s3.S3
closed *atomic.Bool
}
// NewS3 returns configured S3 adapter
func NewS3(s3Config *S3Config) (*S3, error) {
if err := s3Config.Validate(); err != nil {
return nil, err
}
awsConfig := aws.NewConfig().
WithCredentials(credentials.NewStaticCredentials(s3Config.AccessKey, s3Config.SecretKey, "")).
WithRegion(s3Config.Region)
if s3Config.Endpoint != "" {
awsConfig.WithEndpoint(s3Config.Endpoint)
awsConfig.WithS3ForcePathStyle(true)
}
if s3Config.Format == "" {
s3Config.Format = types2.FileFormatNDJSON
}
s3Session, err := session.NewSession()
if err != nil {
return nil, errorj.SaveOnStageError.Wrap(err, "failed to create s3 session")
}
return &S3{AbstractFileAdapter: AbstractFileAdapter{config: &s3Config.FileConfig}, client: s3.New(s3Session, awsConfig), config: s3Config, closed: atomic.NewBool(false)}, nil
}
func (a *S3) UploadBytes(fileName string, fileBytes []byte) error {
return a.Upload(fileName, bytes.NewReader(fileBytes))
}
// Upload creates named file on s3 with payload
func (a *S3) Upload(fileName string, fileReader io.ReadSeeker) error {
fileName = a.Path(fileName)
if a.closed.Load() {
return fmt.Errorf("attempt to use closed S3 instance")
}
params := &s3.PutObjectInput{
Bucket: aws.String(a.config.Bucket),
}
if a.config.Compression == types2.FileCompressionGZIP {
params.ContentType = aws.String("application/gzip")
} else {
switch a.config.Format {
case types2.FileFormatCSV:
params.ContentType = aws.String("text/csv")
case types2.FileFormatNDJSON, types2.FileFormatNDJSONFLAT:
params.ContentType = aws.String("application/x-ndjson")
}
}
params.Key = aws.String(fileName)
params.Body = fileReader
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5)
defer cancel()
if _, err := a.client.PutObjectWithContext(ctx, params); err != nil {
return errorj.SaveOnStageError.Wrap(err, "failed to write file to s3").
WithProperty(errorj.DBInfo, &types2.ErrorPayload{
Bucket: a.config.Bucket,
Statement: fmt.Sprintf("file: %s", fileName),
})
}
return nil
}
// Download downloads file from s3 bucket
func (a *S3) Download(fileName string) ([]byte, error) {
fileName = a.Path(fileName)
if a.closed.Load() {
return nil, fmt.Errorf("attempt to use closed S3 instance")
}
params := &s3.GetObjectInput{
Bucket: aws.String(a.config.Bucket),
Key: aws.String(fileName),
}
resp, err := a.client.GetObject(params)
if err != nil {
return nil, errorj.SaveOnStageError.Wrap(err, "failed to read file from s3").
WithProperty(errorj.DBInfo, &types2.ErrorPayload{
Bucket: a.config.Bucket,
Statement: fmt.Sprintf("file: %s", fileName),
})
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errorj.SaveOnStageError.Wrap(err, "failed to read file from s3").
WithProperty(errorj.DBInfo, &types2.ErrorPayload{
Bucket: a.config.Bucket,
Statement: fmt.Sprintf("file: %s", fileName),
})
}
return data, nil
}
// DeleteObject deletes object from s3 bucket by key
func (a *S3) DeleteObject(key string) error {
key = a.Path(key)
if a.closed.Load() {
return fmt.Errorf("attempt to use closed S3 instance")
}
input := &s3.DeleteObjectInput{Bucket: &a.config.Bucket, Key: &key}
output, err := a.client.DeleteObject(input)
if err != nil {
return errorj.SaveOnStageError.Wrap(err, "failed to delete from s3").
WithProperty(errorj.DBInfo, &types2.ErrorPayload{
Bucket: a.config.Bucket,
Statement: fmt.Sprintf("file: %s", key),
})
}
if output != nil && output.DeleteMarker != nil && !*(output.DeleteMarker) {
return errorj.SaveOnStageError.Wrap(err, "file hasn't been deleted from s3").
WithProperty(errorj.DBInfo, &types2.ErrorPayload{
Bucket: a.config.Bucket,
Statement: fmt.Sprintf("file: %s", key),
})
}
return nil
}
// ValidateWritePermission tries to create temporary file and remove it.
// returns nil if file creation was successful.
func (a *S3) ValidateWritePermission() error {
filename := fmt.Sprintf("test_%v", time.Now())
if err := a.UploadBytes(filename, []byte{}); err != nil {
return err
}
if err := a.DeleteObject(filename); err != nil {
logging.Warnf("Cannot remove object %q from S3: %v", filename, err)
// Suppressing error because we need to check only write permission
// return err
}
return nil
}
// Close returns nil
func (a *S3) Close() error {
a.closed.Store(true)
return nil
}