/
s3.go
198 lines (166 loc) · 5.06 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
package gxutil
import (
"fmt"
"io"
"os"
"sort"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"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"
)
// S3 is a AWS s3 object
type S3 struct {
Bucket string
Region string
}
// WriteStream write to an S3 bucket (upload)
// Example: Database or CSV stream into S3 file
func (s *S3) WriteStream(key string, reader io.Reader) error {
s.GetRegion()
// https://docs.aws.amazon.com/sdk-for-go/api/service/s3/
// The session the S3 Uploader will use
sess := session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewEnvCredentials(),
Region: aws.String(s.Region),
S3ForcePathStyle: aws.Bool(true),
// Endpoint: aws.String(endpoint),
// LogLevel: aws.LogLevel(aws.LogDebugWithHTTPBody),
}))
uploader := s3manager.NewUploader(sess)
uploader.Concurrency = 10
// Upload the file to S3.
_, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(s.Bucket),
Key: aws.String(key),
Body: reader,
})
if err != nil {
return fmt.Errorf("failed to upload file, %v", err)
}
return nil
}
type fakeWriterAt struct {
w io.Writer
}
func (fw fakeWriterAt) WriteAt(p []byte, offset int64) (n int, err error) {
// ignore 'offset' because we forced sequential downloads
return fw.w.Write(p)
}
// ReadStream read from an S3 bucket (download)
// Example: S3 file stream into Database or CSV
func (s *S3) ReadStream(key string) (*io.PipeReader, error) {
s.GetRegion()
// https://docs.aws.amazon.com/sdk-for-go/api/service/s3/
// The session the S3 Downloader will use
sess := session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewEnvCredentials(),
Region: aws.String(s.Region),
S3ForcePathStyle: aws.Bool(true),
// Endpoint: aws.String(endpoint),
// LogLevel: aws.LogLevel(aws.LogDebugWithHTTPBody),
}))
// Create a downloader with the session and default options
downloader := s3manager.NewDownloader(sess)
downloader.Concurrency = 1
pipeR, pipeW := io.Pipe()
go func() {
// Write the contents of S3 Object to the file
_, err := downloader.Download(
fakeWriterAt{pipeW},
&s3.GetObjectInput{
Bucket: aws.String(s.Bucket),
Key: aws.String(key),
})
Check(err, "Error downloading S3 File -> "+key)
pipeW.Close()
}()
return pipeR, nil
}
// GetRegion determines the region of the bucket
func (s *S3) GetRegion() (region string) {
if s.Region != "" {
return s.Region
}
sess := session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewEnvCredentials(),
S3ForcePathStyle: aws.Bool(true),
DisableRestProtocolURICleaning: aws.Bool(true),
}))
region, err := s3manager.GetBucketRegion(aws.BackgroundContext(), sess, s.Bucket, "us-east-1")
if err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "NotFound" {
fmt.Fprintf(os.Stderr, "unable to find bucket %s's region not found\n", s.Bucket)
}
LogError(Error(err, "Bucket not found for "+s.Bucket))
}
s.Region = region
return region
}
// Delete deletes an s3 object at provided key
func (s *S3) Delete(key string) (err error) {
s.GetRegion()
sess := session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewEnvCredentials(),
Region: aws.String(s.Region),
S3ForcePathStyle: aws.Bool(true),
DisableRestProtocolURICleaning: aws.Bool(true),
// Endpoint: aws.String(endpoint),
// LogLevel: aws.LogLevel(aws.LogDebugWithHTTPBody),
}))
// Create S3 service client
svc := s3.New(sess)
paths, err := s.List(key)
if err != nil {
return
}
objects := []*s3.ObjectIdentifier{}
for _, path := range paths {
objects = append(objects, &s3.ObjectIdentifier{Key: aws.String(path)})
}
_, err = svc.DeleteObjects(&s3.DeleteObjectsInput{
Bucket: aws.String(s.Bucket),
Delete: &s3.Delete{
Objects: objects,
Quiet: aws.Bool(true),
},
})
if err != nil {
return Error(err, "Unable to delete S3 object: "+key)
}
err = svc.WaitUntilObjectNotExists(&s3.HeadObjectInput{
Bucket: aws.String(s.Bucket),
Key: aws.String(key),
})
return err
}
// List S3 objects from a key/prefix
func (s *S3) List(key string) (paths []string, err error) {
s.GetRegion()
sess := session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewEnvCredentials(),
Region: aws.String(s.Region),
S3ForcePathStyle: aws.Bool(true),
// Endpoint: aws.String(endpoint),
// LogLevel: aws.LogLevel(aws.LogDebugWithHTTPBody),
}))
// Create S3 service client
svc := s3.New(sess)
input := &s3.ListObjectsV2Input{
Bucket: aws.String(s.Bucket),
Prefix: aws.String(key),
MaxKeys: aws.Int64(100000),
}
result, err := svc.ListObjectsV2(input)
if err != nil {
return paths, err
}
for _, obj := range result.Contents {
// paths = append(paths, F(`s3://%s/%s`, s.Bucket, *obj.Key))
paths = append(paths, *obj.Key)
}
sort.Strings(paths)
return paths, err
}