forked from willnorris/imageproxy
/
s3cache.go
128 lines (111 loc) · 2.87 KB
/
s3cache.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
// Copyright 2013 The imageproxy authors.
// SPDX-License-Identifier: Apache-2.0
// Package s3cache provides an httpcache.Cache implementation that stores
// cached values on Amazon S3.
package s3cache
import (
"bytes"
"crypto/md5"
"encoding/hex"
"errors"
"io"
"log"
"net/url"
"path"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
type cache struct {
*s3.S3
bucket, prefix string
}
func (c *cache) Get(key string) ([]byte, bool) {
key = path.Join(c.prefix, keyToFilename(key))
input := &s3.GetObjectInput{
Bucket: &c.bucket,
Key: &key,
}
resp, err := c.GetObject(input)
if err != nil {
var aerr awserr.Error
if errors.As(err, &aerr) && aerr.Code() != "NoSuchKey" {
log.Printf("error fetching from s3: %v", aerr)
}
return nil, false
}
value, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("error reading s3 response body: %v", err)
return nil, false
}
return value, true
}
func (c *cache) Set(key string, value []byte) {
key = path.Join(c.prefix, keyToFilename(key))
input := &s3.PutObjectInput{
Body: aws.ReadSeekCloser(bytes.NewReader(value)),
Bucket: &c.bucket,
Key: &key,
}
_, err := c.PutObject(input)
if err != nil {
log.Printf("error writing to s3: %v", err)
}
}
func (c *cache) Delete(key string) {
key = path.Join(c.prefix, keyToFilename(key))
input := &s3.DeleteObjectInput{
Bucket: &c.bucket,
Key: &key,
}
_, err := c.DeleteObject(input)
if err != nil {
log.Printf("error deleting from s3: %v", err)
}
}
func keyToFilename(key string) string {
h := md5.New()
_, _ = io.WriteString(h, key)
return hex.EncodeToString(h.Sum(nil))
}
// New constructs a cache configured using the provided URL string. URL should
// be of the form: "s3://region/bucket/optional-path-prefix". Credentials
// should be specified using one of the mechanisms supported by aws-sdk-go (see
// https://docs.aws.amazon.com/sdk-for-go/api/aws/session/).
func New(s string) (*cache, error) {
u, err := url.Parse(s)
if err != nil {
return nil, err
}
region := u.Host
path := strings.SplitN(strings.TrimPrefix(u.Path, "/"), "/", 2)
bucket := path[0]
var prefix string
if len(path) > 1 {
prefix = path[1]
}
config := aws.NewConfig().WithRegion(region)
// allow overriding some additional config options, mostly useful when
// working with s3-compatible services other than AWS.
if v := u.Query().Get("endpoint"); v != "" {
config = config.WithEndpoint(v)
}
if v := u.Query().Get("disableSSL"); v == "1" {
config = config.WithDisableSSL(true)
}
if v := u.Query().Get("s3ForcePathStyle"); v == "1" {
config = config.WithS3ForcePathStyle(true)
}
sess, err := session.NewSession(config)
if err != nil {
return nil, err
}
return &cache{
S3: s3.New(sess),
bucket: bucket,
prefix: prefix,
}, nil
}