/
s3.go
211 lines (176 loc) · 5.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
206
207
208
209
210
211
package s3
import (
"bytes"
"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/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/ecletus/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
}
func EC2RoleAwsConfig(config *Config) *aws.Config {
ec2m := ec2metadata.New(session.New(), &aws.Config{
HTTPClient: &http.Client{Timeout: 10 * time.Second},
Endpoint: aws.String("http://169.254.169.254/latest"),
})
cr := credentials.NewCredentials(&ec2rolecreds.EC2RoleProvider{
Client: ec2m,
})
return &aws.Config{
Region: aws.String(config.Region),
Credentials: cr,
}
}
// New initialize S3 storage
func New(config *Config) *Client {
if config.ACL == "" {
config.ACL = s3.BucketCannedACLPublicRead
}
client := &Client{Config: config}
if config.AccessID == "" && config.AccessKey == "" {
client.S3 = s3.New(session.New(), EC2RoleAwsConfig(config))
} else {
creds := credentials.NewStaticCredentials(config.AccessID, config.AccessKey, config.SessionToken)
if _, err := creds.Get(); err == nil {
client.S3 = s3.New(session.New(), &aws.Config{
Region: &config.Region,
Credentials: creds,
})
}
}
return client
}
// Stat receive file stat by path
func (client Client) Stat(path string) (info *os.FileInfo, notFound bool, err error) {
getResponse, err := client.S3.HeadObject(&s3.GetObjectInput{
Bucket: aws.String(client.Config.Bucket),
Key: aws.String(client.ToRelativePath(path)),
})
if err == nil {
info = &fileStat{name:filepath.Base(path), size: *getResponse.ContentLength,
modTime: *getResponse.LastModified}
} else if strings.Contains(err.Error(), "NoSuchKey") {
return nil, true, nil
}
return
}
// Get receive file with given path
func (client Client) Get(path string) (file *os.File, err error) {
getResponse, err := client.S3.GetObject(&s3.GetObjectInput{
Bucket: aws.String(client.Config.Bucket),
Key: aws.String(client.ToRelativePath(path)),
})
if err == nil {
if file, err = ioutil.TempFile("/tmp", "s3"); err == nil {
_, err = io.Copy(file, getResponse.Body)
file.Seek(0, 0)
}
}
return file, 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),
}
_, 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+)/`)
func (client Client) ToRelativePath(urlPath string) string {
if urlRegexp.MatchString(urlPath) {
if u, err := url.Parse(urlPath); err == nil {
return u.Path
}
}
return "/" + strings.TrimPrefix(urlPath, "/")
}