-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
267 lines (223 loc) · 6.73 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package s3
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/qor/oss"
)
// Client S3 storage
type Client struct {
*s3.S3
Config *Config
}
// Config S3 client config
type Config struct {
AccessID string
AccessKey string
Region string
Bucket string
SessionToken string
ACL string
Endpoint string
S3Endpoint string
S3ForcePathStyle bool
CacheControl string
Session *session.Session
RoleARN string
}
func ec2RoleAwsCreds(config *Config) *credentials.Credentials {
ec2m := ec2metadata.New(session.New(), &aws.Config{
HTTPClient: &http.Client{Timeout: 10 * time.Second},
Endpoint: aws.String("http://169.254.169.254/latest"),
})
return credentials.NewCredentials(&ec2rolecreds.EC2RoleProvider{
Client: ec2m,
})
}
// Does this need to be publicly exported?
func EC2RoleAwsConfig(config *Config) *aws.Config {
return &aws.Config{
Region: aws.String(config.Region),
Credentials: ec2RoleAwsCreds(config),
}
}
// New initialize S3 storage
func New(config *Config) *Client {
if config.ACL == "" {
config.ACL = s3.BucketCannedACLPublicRead
}
client := &Client{Config: config}
if config.RoleARN != "" {
sess := session.Must(session.NewSession())
creds := stscreds.NewCredentials(sess, config.RoleARN)
s3Config := &aws.Config{
Region: &config.Region,
Endpoint: &config.S3Endpoint,
S3ForcePathStyle: &config.S3ForcePathStyle,
Credentials: creds,
}
client.S3 = s3.New(sess, s3Config)
return client
}
s3Config := &aws.Config{
Region: &config.Region,
Endpoint: &config.S3Endpoint,
S3ForcePathStyle: &config.S3ForcePathStyle,
}
if config.Session != nil {
client.S3 = s3.New(config.Session, s3Config)
} else if config.AccessID == "" && config.AccessKey == "" {
// use aws default Credentials
// s3Config.Credentials = ec2RoleAwsCreds(config)
sess := session.Must(session.NewSession())
client.S3 = s3.New(sess, s3Config)
} else {
creds := credentials.NewStaticCredentials(config.AccessID, config.AccessKey, config.SessionToken)
if _, err := creds.Get(); err == nil {
s3Config.Credentials = creds
client.S3 = s3.New(session.New(), s3Config)
}
}
return client
}
// Get receive file with given path
func (client Client) Get(path string) (file *os.File, err error) {
readCloser, err := client.GetStream(path)
ext := filepath.Ext(path)
pattern := fmt.Sprintf("s3*%s", ext)
if err == nil {
if file, err = ioutil.TempFile("/tmp", pattern); err == nil {
defer readCloser.Close()
_, err = io.Copy(file, readCloser)
file.Seek(0, 0)
}
}
return file, err
}
// GetStream get file as stream
func (client Client) GetStream(path string) (io.ReadCloser, error) {
getResponse, err := client.S3.GetObject(&s3.GetObjectInput{
Bucket: aws.String(client.Config.Bucket),
Key: aws.String(client.ToRelativePath(path)),
})
return getResponse.Body, err
}
// Put store a reader into given path
func (client Client) Put(urlPath string, reader io.Reader) (*oss.Object, error) {
if seeker, ok := reader.(io.ReadSeeker); ok {
seeker.Seek(0, 0)
}
urlPath = client.ToRelativePath(urlPath)
buffer, err := ioutil.ReadAll(reader)
fileType := mime.TypeByExtension(path.Ext(urlPath))
if fileType == "" {
fileType = http.DetectContentType(buffer)
}
params := &s3.PutObjectInput{
Bucket: aws.String(client.Config.Bucket), // required
Key: aws.String(urlPath), // required
ACL: aws.String(client.Config.ACL),
Body: bytes.NewReader(buffer),
ContentLength: aws.Int64(int64(len(buffer))),
ContentType: aws.String(fileType),
}
if client.Config.CacheControl != "" {
params.CacheControl = aws.String(client.Config.CacheControl)
}
_, err = client.S3.PutObject(params)
now := time.Now()
return &oss.Object{
Path: urlPath,
Name: filepath.Base(urlPath),
LastModified: &now,
StorageInterface: client,
}, err
}
// Delete delete file
func (client Client) Delete(path string) error {
_, err := client.S3.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(client.Config.Bucket),
Key: aws.String(client.ToRelativePath(path)),
})
return err
}
// List list all objects under current path
func (client Client) List(path string) ([]*oss.Object, error) {
var objects []*oss.Object
var prefix string
if path != "" {
prefix = strings.Trim(path, "/") + "/"
}
listObjectsResponse, err := client.S3.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(client.Config.Bucket),
Prefix: aws.String(prefix),
})
if err == nil {
for _, content := range listObjectsResponse.Contents {
objects = append(objects, &oss.Object{
Path: client.ToRelativePath(*content.Key),
Name: filepath.Base(*content.Key),
LastModified: content.LastModified,
StorageInterface: client,
})
}
}
return objects, err
}
// GetEndpoint get endpoint, FileSystem's endpoint is /
func (client Client) GetEndpoint() string {
if client.Config.Endpoint != "" {
return client.Config.Endpoint
}
endpoint := client.S3.Endpoint
for _, prefix := range []string{"https://", "http://"} {
endpoint = strings.TrimPrefix(endpoint, prefix)
}
return client.Config.Bucket + "." + endpoint
}
var urlRegexp = regexp.MustCompile(`(https?:)?//((\w+).)+(\w+)/`)
// ToRelativePath process path to relative path
func (client Client) ToRelativePath(urlPath string) string {
if urlRegexp.MatchString(urlPath) {
if u, err := url.Parse(urlPath); err == nil {
if client.Config.S3ForcePathStyle { // First part of path will be bucket name
return strings.TrimPrefix(u.Path, "/"+client.Config.Bucket)
}
return u.Path
}
}
if client.Config.S3ForcePathStyle { // First part of path will be bucket name
return "/" + strings.TrimPrefix(urlPath, "/"+client.Config.Bucket+"/")
}
return "/" + strings.TrimPrefix(urlPath, "/")
}
// GetURL get public accessible URL
func (client Client) GetURL(path string) (url string, err error) {
if client.Endpoint == "" {
if client.Config.ACL == s3.BucketCannedACLPrivate || client.Config.ACL == s3.BucketCannedACLAuthenticatedRead {
getResponse, _ := client.S3.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(client.Config.Bucket),
Key: aws.String(client.ToRelativePath(path)),
})
return getResponse.Presign(1 * time.Hour)
}
}
return path, nil
}