/
aws.go
172 lines (155 loc) · 4.64 KB
/
aws.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
package aws
import (
"context"
"crypto/md5"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"path"
"time"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/carlmjohnson/errorx"
"gocloud.dev/blob"
"github.com/spotlightpa/almanack/internal/httpx"
"github.com/spotlightpa/almanack/internal/must"
"github.com/spotlightpa/almanack/pkg/almlog"
)
func AddFlags(fl *flag.FlagSet) func() (imageStore, fileStore BlobStore) {
accessKeyID := fl.String("aws-access-key", "", "AWS access `key` ID")
secretAccessKey := fl.String("aws-secret-key", "", "AWS secret access `key`")
region := fl.String("aws-s3-region", "us-east-2", "AWS `region` to use for S3")
ibucket := fl.String("image-bucket-url", "mem://", "bucket `URL` for images")
fbucket := fl.String("file-bucket-url", "mem://", "bucket `URL` for files")
return func() (imageStore, fileStore BlobStore) {
err := register("s3-cli", *region, *accessKeyID, *secretAccessKey)
if err != nil {
almlog.Logger.Error("aws.register", "err", err)
}
imageStore = BlobStore{*ibucket}
if *ibucket == "mem://" {
almlog.Logger.Warn("mocking AWS image bucket")
}
fileStore = BlobStore{*fbucket}
if *fbucket == "mem://" {
almlog.Logger.Warn("mocking AWS file bucket")
}
return
}
}
type BlobStore struct {
bucket string
}
func NewBlobStore(s string) BlobStore {
return BlobStore{s}
}
func (bs BlobStore) SignPutURL(ctx context.Context, srcPath string, h http.Header) (signedURL string, err error) {
l := almlog.FromContext(ctx)
l.InfoContext(ctx, "aws.SignPutURL", "url", srcPath)
b, err := blob.OpenBucket(ctx, bs.bucket)
if err != nil {
return "", err
}
defer b.Close()
return b.SignedURL(ctx, srcPath, &blob.SignedURLOptions{
Expiry: 15 * time.Minute,
Method: http.MethodPut,
ContentType: h.Get("Content-Type"),
EnforceAbsentContentType: true,
BeforeSign: func(as func(any) bool) error {
var opts *s3.PutObjectInput
if as(&opts) {
if disposition := h.Get("Content-Disposition"); disposition != "" {
opts.ContentDisposition = &disposition
}
if cc := h.Get("Cache-Control"); cc != "" {
opts.CacheControl = &cc
}
}
return nil
},
})
}
func (bs BlobStore) SignGetURL(ctx context.Context, srcPath string) (signedURL string, err error) {
l := almlog.FromContext(ctx)
l.InfoContext(ctx, "aws.SignGetURL", "url", srcPath)
b, err := blob.OpenBucket(ctx, bs.bucket)
if err != nil {
return "", err
}
defer b.Close()
return b.SignedURL(ctx, srcPath, &blob.SignedURLOptions{
Expiry: 24 * time.Hour,
Method: http.MethodGet,
BeforeSign: func(as func(any) bool) error {
var opts *s3.GetObjectInput
if as(&opts) {
filename := path.Base(srcPath)
download := httpx.AttachmentName(filename)
opts.ResponseContentDisposition = &download
}
return nil
},
})
}
func (bs BlobStore) BuildURL(srcPath string) string {
u := must.Get(url.Parse(bs.bucket))
// Just assuming bucket name is valid DNS…
return fmt.Sprintf("https://%s/%s", u.Hostname(), srcPath)
}
func (bs BlobStore) WriteFile(ctx context.Context, path string, h http.Header, data []byte) (err error) {
l := almlog.FromContext(ctx)
b, err := blob.OpenBucket(ctx, bs.bucket)
if err != nil {
return err
}
defer errorx.Defer(&err, b.Close)
var checksum []byte
// If attrs + MD5 match skip
if attrs, err := b.Attributes(ctx, path); err == nil &&
attrs.MD5 != nil &&
attrs.CacheControl == h.Get("Cache-Control") &&
attrs.ContentType == h.Get("Content-Type") &&
attrs.ContentDisposition == h.Get("Content-Disposition") {
// Get checksum
a := md5.Sum(data)
checksum = a[:]
if string(checksum) == string(attrs.MD5) {
l.InfoContext(ctx, "aws.WriteFile: skipping; already uploaded",
"bucket", bs.bucket, "path", path)
return nil
}
}
l.InfoContext(ctx, "aws.WriteFile: writing", "bucket", bs.bucket, "path", path)
return b.WriteAll(ctx, path, data, &blob.WriterOptions{
CacheControl: h.Get("Cache-Control"),
ContentType: h.Get("Content-Type"),
ContentDisposition: h.Get("Content-Disposition"),
ContentMD5: checksum,
})
}
func (bs BlobStore) ReadMD5(ctx context.Context, path string) (hash []byte, size int64, err error) {
l := almlog.FromContext(ctx)
b, err := blob.OpenBucket(ctx, bs.bucket)
if err != nil {
return nil, 0, err
}
defer errorx.Defer(&err, b.Close)
attrs, err := b.Attributes(ctx, path)
if err != nil {
return nil, 0, err
}
if attrs.MD5 != nil {
return attrs.MD5, attrs.Size, nil
}
l.InfoContext(ctx, "ReadMD5: missing MD5 attribute")
r, err := b.NewReader(ctx, path, nil)
if err != nil {
return nil, 0, err
}
h := md5.New()
size, err = io.Copy(h, r)
hash = h.Sum(nil)
return
}