forked from SkygearIO/skygear-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
222 lines (195 loc) · 5.45 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
// Copyright 2015-present Oursky Ltd.
//
// 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 asset
import (
"errors"
"fmt"
"io"
"strconv"
"strings"
"time"
"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"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
// s3Store implements Store by storing files on S3
type s3Store struct {
svc *s3.S3
uploader *s3manager.Uploader
bucket *string
urlPrefix string
public bool
}
// NewS3Store returns a new s3Store
func NewS3Store(
accessKey string,
secretKey string,
regionName string,
bucketName string,
urlPrefix string,
public bool,
) (Store, error) {
creds := credentials.NewStaticCredentials(
accessKey,
secretKey,
"",
)
sess := session.Must(session.NewSession())
svc := s3.New(sess, &aws.Config{
Region: aws.String(regionName),
Credentials: creds,
})
uploader := s3manager.NewUploaderWithClient(svc)
bucket := aws.String(bucketName)
return &s3Store{
svc: svc,
uploader: uploader,
bucket: bucket,
urlPrefix: urlPrefix,
public: public,
}, nil
}
// GetFileReader returns a reader for files
func (s *s3Store) GetFileReader(name string) (io.ReadCloser, error) {
key := aws.String(name)
input := &s3.GetObjectInput{
Bucket: s.bucket,
Key: key,
}
objOutput, err := s.svc.GetObject(input)
return objOutput.Body, err
}
func (s *s3Store) GetRangedFileReader(
name string,
fileRange FileRange,
) (*FileRangedGetResult, error) {
key := aws.String(name)
rangeHeader := fmt.Sprintf("bytes=%d-%d", fileRange.From, fileRange.To)
input := &s3.GetObjectInput{
Bucket: s.bucket,
Key: key,
Range: &rangeHeader,
}
output, err := s.svc.GetObject(input)
if err != nil {
return nil, err
}
if output.ContentRange == nil {
return nil, errors.New("missing content ranges header")
}
acceptedRange, totalSize, err := parseContentRange(*output.ContentRange)
if err != nil {
return nil, err
}
return &FileRangedGetResult{
ReadCloser: output.Body,
AcceptedRange: acceptedRange,
TotalSize: totalSize,
}, nil
}
// PutFileReader uploads a file to s3 with content from io.Reader
func (s *s3Store) PutFileReader(
name string,
src io.Reader,
length int64,
contentType string,
) error {
key := aws.String(name)
input := &s3manager.UploadInput{
Body: src,
Bucket: s.bucket,
Key: key,
ContentType: aws.String(contentType),
}
_, err := s.uploader.Upload(input)
return err
}
// GeneratePostFileRequest return a PostFileRequest for uploading asset
func (s *s3Store) GeneratePostFileRequest(name string, contentType string, length int64) (*PostFileRequest, error) {
return &PostFileRequest{
Action: "/files/" + name,
}, nil
}
// SignedURL return a signed s3 URL with expiry date
func (s *s3Store) SignedURL(name string) (string, error) {
if !s.IsSignatureRequired() {
if s.urlPrefix != "" {
return strings.Join([]string{s.urlPrefix, name}, "/"), nil
}
key := aws.String(name)
input := &s3.GetObjectInput{
Bucket: s.bucket,
Key: key,
}
req, _ := s.svc.GetObjectRequest(input)
// Sign will interpolate the URL String, otherwise the URL will be %bucket%
req.Sign()
return req.HTTPRequest.URL.String(), nil
}
key := aws.String(name)
input := &s3.GetObjectInput{
Bucket: s.bucket,
Key: key,
}
req, _ := s.svc.GetObjectRequest(input)
return req.Presign(time.Minute * time.Duration(15))
}
// IsSignatureRequired indicates whether a signature is required
func (s *s3Store) IsSignatureRequired() bool {
return !s.public
}
// ParseSignature tries to parse the asset signature
func (s *s3Store) ParseSignature(
signed string,
name string,
expiredAt time.Time,
) (bool, error) {
return false, errors.New(
"Asset signature parsing for s3-based asset store is not available",
)
}
// parseContentRange parses the content range string
// in the format of something like `bytes 123-567/1024`
//
func parseContentRange(contentRangeString string) (FileRange, int64, error) {
splits := strings.SplitN(contentRangeString, " ", 2)
if len(splits) != 2 {
return FileRange{}, 0, errors.New("content range is malformed")
}
if strings.ToLower(splits[0]) != "bytes" {
return FileRange{}, 0, errors.New(
"only support content range in unit of bytes",
)
}
compSplits := strings.SplitN(splits[1], "/", 2)
if len(compSplits) != 2 {
return FileRange{}, 0, errors.New("content range is malformed")
}
rangeSplits := strings.SplitN(compSplits[0], "-", 2)
if len(rangeSplits) != 2 {
return FileRange{}, 0, errors.New("content range is malformed")
}
rangeFrom, err1 := strconv.ParseInt(rangeSplits[0], 10, 64)
rangeTo, err2 := strconv.ParseInt(rangeSplits[1], 10, 64)
totalSize, err3 := strconv.ParseInt(compSplits[1], 10, 64)
if err1 != nil || err2 != nil || err3 != nil {
return FileRange{}, 0, errors.New("content range is malformed")
}
return FileRange{
From: rangeFrom,
To: rangeTo,
}, totalSize, nil
}