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
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
*/
|
2016-03-14 01:59:26 +00:00
|
|
|
package s3 // import "camlistore.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
|
|
|
|
2014-10-18 19:55:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/blobserver"
|
2014-10-18 17:22:35 +00:00
|
|
|
"camlistore.org/pkg/blobserver/memory"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.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" {
|
|
|
|
return nil, fmt.Errorf("Bucket %q doesn't exist.", sto.bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|