-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.go
42 lines (33 loc) · 868 Bytes
/
writer.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
package s3files
import (
"bytes"
"context"
"net/url"
"github.com/puellanivis/breton/lib/files"
"github.com/puellanivis/breton/lib/files/wrapper"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
)
func (h *handler) Create(ctx context.Context, uri *url.URL) (files.Writer, error) {
bucket, key, err := getBucketKey("create", uri)
if err != nil {
return nil, err
}
w := wrapper.NewWriter(ctx, uri, func(b []byte) error {
cl, err := h.getClient(ctx, bucket)
if err != nil {
return files.PathError("sync", uri.String(), err)
}
req := &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: bytes.NewReader(b),
}
_, err = cl.PutObjectWithContext(ctx, req)
if err != nil {
return files.PathError("sync", uri.String(), normalizeError(err))
}
return nil
})
return w, nil
}