2011-03-28 05:06:29 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2013-07-08 01:52:14 +00:00
|
|
|
/*
|
|
|
|
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_access_key": "...",
|
|
|
|
"aws_secret_access_key": "...",
|
|
|
|
"skipStartupCheck": false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
*/
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
package s3 // import "perkeep.org/pkg/blobserver/s3"
|
2011-03-28 05:06:29 +00:00
|
|
|
|
|
|
|
import (
|
2011-04-03 15:07:40 +00:00
|
|
|
"fmt"
|
2014-05-08 22:31:42 +00:00
|
|
|
"log"
|
|
|
|
"strings"
|
2011-04-03 15:07:40 +00:00
|
|
|
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
"perkeep.org/pkg/blob"
|
|
|
|
"perkeep.org/pkg/blobserver"
|
|
|
|
"perkeep.org/pkg/blobserver/memory"
|
|
|
|
"perkeep.org/pkg/misc/amazon/s3"
|
2015-12-16 18:24:01 +00:00
|
|
|
|
|
|
|
"go4.org/fault"
|
2015-12-01 16:19:49 +00:00
|
|
|
"go4.org/jsonconfig"
|
2016-01-22 21:08:46 +00:00
|
|
|
"go4.org/syncutil"
|
2011-03-28 05:06:29 +00:00
|
|
|
)
|
|
|
|
|
2014-10-18 19:55:30 +00:00
|
|
|
var (
|
|
|
|
_ blob.SubFetcher = (*s3Storage)(nil)
|
|
|
|
_ blobserver.MaxEnumerateConfig = (*s3Storage)(nil)
|
|
|
|
)
|
|
|
|
|
2014-03-17 02:39:43 +00:00
|
|
|
var (
|
|
|
|
faultReceive = fault.NewInjector("s3_receive")
|
|
|
|
faultEnumerate = fault.NewInjector("s3_enumerate")
|
|
|
|
faultStat = fault.NewInjector("s3_stat")
|
|
|
|
faultGet = fault.NewInjector("s3_get")
|
|
|
|
)
|
|
|
|
|
2016-01-22 21:08:46 +00:00
|
|
|
const maxParallelHTTP = 5
|
|
|
|
|
2011-03-28 05:06:29 +00:00
|
|
|
type s3Storage struct {
|
2011-04-03 15:07:40 +00:00
|
|
|
s3Client *s3.Client
|
2011-04-04 02:15:09 +00:00
|
|
|
bucket string
|
2015-12-16 22:25:23 +00:00
|
|
|
// 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 string
|
|
|
|
cache *memory.Storage // or nil for no cache
|
2014-03-05 16:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *s3Storage) String() string {
|
2015-12-16 22:25:23 +00:00
|
|
|
if s.dirPrefix != "" {
|
|
|
|
return fmt.Sprintf("\"s3\" blob storage at host %q, bucket %q, directory %q", s.hostname, s.bucket, s.dirPrefix)
|
|
|
|
}
|
2014-03-05 16:23:42 +00:00
|
|
|
return fmt.Sprintf("\"s3\" blob storage at host %q, bucket %q", s.hostname, s.bucket)
|
2011-03-28 05:06:29 +00:00
|
|
|
}
|
|
|
|
|
2013-09-01 16:50:35 +00:00
|
|
|
func newFromConfig(_ blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) {
|
2014-03-05 16:23:42 +00:00
|
|
|
hostname := config.OptionalString("hostname", "s3.amazonaws.com")
|
2014-10-18 17:22:35 +00:00
|
|
|
cacheSize := config.OptionalInt64("cacheSize", 32<<20)
|
2011-04-03 15:07:40 +00:00
|
|
|
client := &s3.Client{
|
|
|
|
Auth: &s3.Auth{
|
|
|
|
AccessKey: config.RequiredString("aws_access_key"),
|
|
|
|
SecretAccessKey: config.RequiredString("aws_secret_access_key"),
|
2014-03-05 16:23:42 +00:00
|
|
|
Hostname: hostname,
|
2011-04-03 15:07:40 +00:00
|
|
|
},
|
2016-01-22 21:08:46 +00:00
|
|
|
PutGate: syncutil.NewGate(maxParallelHTTP),
|
2011-04-03 15:07:40 +00:00
|
|
|
}
|
2015-12-16 22:25:23 +00:00
|
|
|
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 += "/"
|
|
|
|
}
|
2011-04-03 15:07:40 +00:00
|
|
|
sto := &s3Storage{
|
2015-12-16 22:25:23 +00:00
|
|
|
s3Client: client,
|
|
|
|
bucket: bucket,
|
|
|
|
dirPrefix: dirPrefix,
|
|
|
|
hostname: hostname,
|
2011-04-03 15:07:40 +00:00
|
|
|
}
|
2011-04-04 02:57:30 +00:00
|
|
|
skipStartupCheck := config.OptionalBool("skipStartupCheck", false)
|
2011-04-03 15:07:40 +00:00
|
|
|
if err := config.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-18 17:22:35 +00:00
|
|
|
if cacheSize != 0 {
|
|
|
|
sto.cache = memory.NewCache(cacheSize)
|
|
|
|
}
|
2011-04-04 02:57:30 +00:00
|
|
|
if !skipStartupCheck {
|
2014-05-08 22:31:42 +00:00
|
|
|
_, err := client.ListBucket(sto.bucket, "", 1)
|
|
|
|
if serr, ok := err.(*s3.Error); ok {
|
|
|
|
if serr.AmazonCode == "NoSuchBucket" {
|
2017-12-10 09:13:00 +00:00
|
|
|
return nil, fmt.Errorf("bucket %q doesn't exist", sto.bucket)
|
2014-05-08 22:31:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This code appears when the hostname has dots in it:
|
|
|
|
if serr.AmazonCode == "PermanentRedirect" {
|
|
|
|
loc, lerr := client.BucketLocation(sto.bucket)
|
|
|
|
if lerr != nil {
|
|
|
|
return nil, fmt.Errorf("Wrong server for bucket %q; and error determining bucket's location: %v", sto.bucket, lerr)
|
|
|
|
}
|
|
|
|
client.Auth.Hostname = loc
|
|
|
|
_, err = client.ListBucket(sto.bucket, "", 1)
|
|
|
|
if err == nil {
|
|
|
|
log.Printf("Warning: s3 server should be %q, not %q. Change config file to avoid start-up latency.", client.Auth.Hostname, hostname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This path occurs when the user set the
|
|
|
|
// wrong server, or didn't set one at all, but
|
|
|
|
// the bucket doesn't have dots in it:
|
|
|
|
if serr.UseEndpoint != "" {
|
|
|
|
// UseEndpoint will be e.g. "brads3test-ca.s3-us-west-1.amazonaws.com"
|
|
|
|
// But we only want the "s3-us-west-1.amazonaws.com" part.
|
|
|
|
client.Auth.Hostname = strings.TrimPrefix(serr.UseEndpoint, sto.bucket+".")
|
|
|
|
_, err = client.ListBucket(sto.bucket, "", 1)
|
|
|
|
if err == nil {
|
|
|
|
log.Printf("Warning: s3 server should be %q, not %q. Change config file to avoid start-up latency.", client.Auth.Hostname, hostname)
|
|
|
|
}
|
|
|
|
}
|
2013-01-20 19:33:41 +00:00
|
|
|
}
|
2014-05-08 22:31:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error listing bucket %s: %v", sto.bucket, err)
|
2013-01-20 19:33:41 +00:00
|
|
|
}
|
2011-04-03 15:07:40 +00:00
|
|
|
}
|
|
|
|
return sto, nil
|
2011-04-02 03:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2011-04-02 05:13:55 +00:00
|
|
|
blobserver.RegisterStorageConstructor("s3", blobserver.StorageConstructor(newFromConfig))
|
2011-04-02 03:45:40 +00:00
|
|
|
}
|