forked from nhost/hasura-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
286 lines (254 loc) · 7.01 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package storage
import (
"context"
"encoding/xml"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/nhost/hasura-storage/controller"
"github.com/sirupsen/logrus"
)
func deptr[T any](p *T) T { //nolint:ireturn
if p == nil {
return *new(T)
}
return *p
}
type S3Error struct {
Code string `xml:"Code"`
Message string `xml:"Message"`
}
func parseS3Error(resp *http.Response) *controller.APIError {
var s3Error S3Error
if err := xml.NewDecoder(resp.Body).Decode(&s3Error); err != nil {
b, err := io.ReadAll(resp.Body)
if err != nil {
return controller.InternalServerError(
fmt.Errorf("problem reading S3 error, status code %d: %w", resp.StatusCode, err),
)
}
return controller.InternalServerError(
fmt.Errorf( //nolint: goerr113
"problem parsing S3 error, status code %d: %s",
resp.StatusCode,
b,
),
)
}
return controller.NewAPIError(
resp.StatusCode,
s3Error.Message,
errors.New(s3Error.Message), //nolint: goerr113
nil,
)
}
type S3 struct {
client *s3.Client
bucket *string
rootFolder string
url string
logger *logrus.Logger
}
func NewS3(
client *s3.Client,
bucket string,
rootFolder string,
url string,
logger *logrus.Logger,
) *S3 {
return &S3{
client: client,
bucket: aws.String(bucket),
rootFolder: rootFolder,
url: url,
logger: logger,
}
}
func (s *S3) PutFile(
ctx context.Context,
content io.ReadSeeker,
filepath string,
contentType string,
) (string, *controller.APIError) {
key, err := url.JoinPath(s.rootFolder, filepath)
if err != nil {
return "", controller.InternalServerError(fmt.Errorf("problem joining path: %w", err))
}
// let's make sure we are in the beginning of the content
if _, err := content.Seek(0, 0); err != nil {
return "", controller.InternalServerError(
fmt.Errorf("problem going to the beginning of the content: %w", err),
)
}
object, err := s.client.PutObject(ctx,
&s3.PutObjectInput{
Body: content,
Bucket: s.bucket,
Key: aws.String(key),
ContentType: aws.String(contentType),
},
)
if err != nil {
return "", controller.InternalServerError(fmt.Errorf("problem putting object: %w", err))
}
return *object.ETag, nil
}
func (s *S3) GetFile(
ctx context.Context,
filepath string,
headers http.Header,
) (*controller.File, *controller.APIError) {
key, err := url.JoinPath(s.rootFolder, filepath)
if err != nil {
return nil, controller.InternalServerError(fmt.Errorf("problem joining path: %w", err))
}
object, err := s.client.GetObject(ctx,
&s3.GetObjectInput{
Bucket: s.bucket,
Key: aws.String(key),
// IfMatch: new(string),
// IfModifiedSince: &time.Time{},
// IfNoneMatch: new(string),
// IfUnmodifiedSince: &time.Time{},
Range: aws.String(headers.Get("range")),
},
)
if err != nil {
return nil, controller.InternalServerError(fmt.Errorf("problem getting object: %w", err))
}
status := http.StatusOK
respHeaders := make(http.Header)
if object.ContentRange != nil {
respHeaders = http.Header{
"Accept-Ranges": []string{"bytes"},
}
respHeaders["Content-Range"] = []string{*object.ContentRange}
status = http.StatusPartialContent
}
return &controller.File{
ContentType: *object.ContentType,
ContentLength: deptr(object.ContentLength),
Etag: *object.ETag,
StatusCode: status,
Body: object.Body,
ExtraHeaders: respHeaders,
}, nil
}
func (s *S3) CreatePresignedURL(
ctx context.Context,
filepath string,
expire time.Duration,
) (string, *controller.APIError) {
key, err := url.JoinPath(s.rootFolder, filepath)
if err != nil {
return "", controller.InternalServerError(fmt.Errorf("problem joining path: %w", err))
}
presignClient := s3.NewPresignClient(s.client)
request, err := presignClient.PresignGetObject(ctx,
&s3.GetObjectInput{ //nolint:exhaustivestruct
Bucket: s.bucket,
Key: aws.String(key),
},
func(po *s3.PresignOptions) {
po.Expires = expire
},
)
if err != nil {
return "", controller.InternalServerError(
fmt.Errorf("problem generating pre-signed URL: %w", err),
)
}
parts := strings.Split(request.URL, "?")
if len(parts) != 2 { //nolint: gomnd
return "", controller.InternalServerError(
fmt.Errorf("problem generating pre-signed URL: %w", err),
)
}
return parts[1], nil
}
func (s *S3) GetFileWithPresignedURL(
ctx context.Context, filepath, signature string, headers http.Header,
) (*controller.File, *controller.APIError) {
if s.rootFolder != "" {
filepath = s.rootFolder + "/" + filepath
}
url := fmt.Sprintf("%s/%s/%s?%s", s.url, *s.bucket, filepath, signature)
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, controller.InternalServerError(fmt.Errorf("problem creating request: %w", err))
}
req.Header = headers
client := http.Client{}
resp, err := client.Do(req) //nolint:bodyclose //we are actually returning the body
if err != nil {
return nil, controller.InternalServerError(fmt.Errorf("problem getting file: %w", err))
}
if !(resp.StatusCode == http.StatusOK ||
resp.StatusCode == http.StatusPartialContent ||
resp.StatusCode == http.StatusNotModified) {
return nil, parseS3Error(resp)
}
respHeaders := make(http.Header)
var length int64
switch resp.StatusCode {
case http.StatusOK, http.StatusPartialContent:
respHeaders = http.Header{
"Accept-Ranges": []string{"bytes"},
}
if resp.StatusCode == http.StatusPartialContent {
respHeaders["Content-Range"] = []string{resp.Header.Get("Content-Range")}
}
length, err = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 32)
if err != nil {
return nil, controller.InternalServerError(
fmt.Errorf("problem parsing Content-Length: %w", err),
)
}
}
return &controller.File{
ContentType: resp.Header.Get("Content-Type"),
ContentLength: length,
Etag: resp.Header.Get("Etag"),
StatusCode: resp.StatusCode,
Body: resp.Body,
ExtraHeaders: respHeaders,
}, nil
}
func (s *S3) DeleteFile(ctx context.Context, filepath string) *controller.APIError {
key, err := url.JoinPath(s.rootFolder, filepath)
if err != nil {
return controller.InternalServerError(fmt.Errorf("problem joining path: %w", err))
}
if _, err := s.client.DeleteObject(ctx,
&s3.DeleteObjectInput{
Bucket: s.bucket,
Key: aws.String(key),
}); err != nil {
return controller.InternalServerError(fmt.Errorf("problem deleting file in s3: %w", err))
}
return nil
}
func (s *S3) ListFiles(ctx context.Context) ([]string, *controller.APIError) {
objects, err := s.client.ListObjects(ctx,
&s3.ListObjectsInput{
Bucket: s.bucket,
Prefix: aws.String(s.rootFolder + "/"),
})
if err != nil {
return nil, controller.InternalServerError(
fmt.Errorf("problem listing objects in s3: %w", err),
)
}
res := make([]string, len(objects.Contents))
for i, c := range objects.Contents {
res[i] = strings.TrimPrefix(*c.Key, s.rootFolder+"/")
}
return res, nil
}