-
Notifications
You must be signed in to change notification settings - Fork 445
/
s3.go
189 lines (166 loc) · 5.78 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
/*
Copyright 2011 The Perkeep Authors
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 s3 registers the "s3" blobserver storage type, storing
blobs in an Amazon Web Services' S3 storage bucket.
Example low-level config:
"/r1/": {
"handler": "storage-s3",
"handlerArgs": {
"bucket": "foo",
"aws_region": "us-east-1",
"aws_access_key": "...",
"aws_secret_access_key": "...",
"skipStartupCheck": false
}
},
*/
package s3 // import "perkeep.org/pkg/blobserver/s3"
import (
"context"
"fmt"
"net/http"
"strings"
"perkeep.org/pkg/blob"
"perkeep.org/pkg/blobserver"
"perkeep.org/pkg/blobserver/memory"
"perkeep.org/pkg/blobserver/proxycache"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"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/s3iface"
"go4.org/fault"
"go4.org/jsonconfig"
)
var (
_ blob.SubFetcher = (*s3Storage)(nil)
_ blobserver.MaxEnumerateConfig = (*s3Storage)(nil)
)
var (
faultReceive = fault.NewInjector("s3_receive")
faultEnumerate = fault.NewInjector("s3_enumerate")
faultStat = fault.NewInjector("s3_stat")
faultGet = fault.NewInjector("s3_get")
)
type s3Storage struct {
client s3iface.S3API
bucket string
// optional "directory" where the blobs are stored, instead of at the root of the bucket.
// S3 is actually flat, which in effect just means that all the objects should have this
// dirPrefix as a prefix of their key.
// If non empty, it should be a slash separated path with a trailing slash and no starting
// slash.
dirPrefix string
// hostname indicates the hostname of the server providing an S3 compatible endpoint.
// It should not be set for AWS's S3 since the correct endpoint will be
// automatically identified based on the bucket name (and, if provided, the
// 'aws_region' low-level config option).
hostname string
}
func (sto *s3Storage) String() string {
if sto.dirPrefix != "" {
return fmt.Sprintf("\"S3\" blob storage at host %q, bucket %q, directory %q", sto.hostname, sto.bucket, sto.dirPrefix)
}
return fmt.Sprintf("\"S3\" blob storage at host %q, bucket %q", sto.hostname, sto.bucket)
}
func newFromConfig(l blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) {
return newFromConfigWithTransport(l, config, nil)
}
// newFromConfigWithTransport constructs a s3 blobserver using the given
// transport for all s3 requests. The transport may be set to 'nil' to use a
// default transport.
// This is used for unit tests.
func newFromConfigWithTransport(_ blobserver.Loader, config jsonconfig.Obj, transport http.RoundTripper) (blobserver.Storage, error) {
hostname := config.OptionalString("hostname", "")
region := config.OptionalString("aws_region", "us-east-1")
cacheSize := config.OptionalInt64("cacheSize", 32<<20)
s3Cfg := aws.NewConfig().WithCredentials(credentials.NewStaticCredentials(
config.RequiredString("aws_access_key"),
config.RequiredString("aws_secret_access_key"),
"",
))
if hostname != "" {
s3Cfg.WithEndpoint(hostname)
}
s3Cfg.WithRegion(region)
if transport != nil {
httpClient := *http.DefaultClient
httpClient.Transport = transport
s3Cfg.WithHTTPClient(&httpClient)
}
awsSession, err := session.NewSession(s3Cfg)
if err != nil {
return nil, err
}
bucket := config.RequiredString("bucket")
var dirPrefix string
if parts := strings.SplitN(bucket, "/", 2); len(parts) > 1 {
dirPrefix = parts[1]
bucket = parts[0]
}
if dirPrefix != "" && !strings.HasSuffix(dirPrefix, "/") {
dirPrefix += "/"
}
skipStartupCheck := config.OptionalBool("skipStartupCheck", false)
if err := config.Validate(); err != nil {
return nil, err
}
ctx := context.TODO() // TODO: 5 min timeout or something?
if !skipStartupCheck {
info, err := normalizeBucketLocation(ctx, awsSession, hostname, bucket, region)
if err != nil {
return nil, err
}
awsSession.Config.WithRegion(info.region)
awsSession.Config.WithEndpoint(info.endpoint)
if !info.isAWS {
awsSession.Config.WithS3ForcePathStyle(true)
}
} else {
// safer default if we can't determine more info
awsSession.Config.WithS3ForcePathStyle(true)
}
sto := &s3Storage{
client: s3.New(awsSession),
bucket: bucket,
dirPrefix: dirPrefix,
hostname: hostname,
}
if cacheSize != 0 {
// This has two layers of LRU caching (proxycache and memory).
// We make the outer one 4x the size so that it doesn't evict from the
// underlying one when it's about to perform its own eviction.
return proxycache.New(cacheSize<<2, memory.NewCache(cacheSize), sto), nil
}
return sto, nil
}
func init() {
blobserver.RegisterStorageConstructor("s3", blobserver.StorageConstructor(newFromConfig))
blobserver.RegisterStorageConstructor("b2", blobserver.StorageConstructor(newFromConfig))
}
// isNotFound checks for s3 errors which indicate the object doesn't exist.
func isNotFound(err error) bool {
if err == nil {
return false
}
if aerr, ok := err.(awserr.Error); ok {
return aerr.Code() == s3.ErrCodeNoSuchKey ||
// Check 'NotFound' as well because it's returned for some requests, even
// though the API model doesn't include it (hence why there isn't an
// 's3.ErrCodeNotFound' for comparison)
aerr.Code() == "NotFound"
}
return false
}