restic/internal/backend/s3/config.go

119 lines
3.5 KiB
Go
Raw Normal View History

2015-12-28 14:51:24 +00:00
package s3
import (
2015-12-28 17:23:02 +00:00
"net/url"
"os"
"path"
2015-12-28 14:51:24 +00:00
"strings"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/options"
2015-12-28 14:51:24 +00:00
)
// Config contains all configuration necessary to connect to an s3 compatible
// server.
type Config struct {
Endpoint string
UseHTTP bool
KeyID string
Secret options.SecretString
Bucket string
Prefix string
Layout string `option:"layout" help:"use this backend layout (default: auto-detect)"`
StorageClass string `option:"storage-class" help:"set S3 storage class (STANDARD, STANDARD_IA, ONEZONE_IA, INTELLIGENT_TIERING or REDUCED_REDUNDANCY)"`
2017-06-05 22:17:39 +00:00
Connections uint `option:"connections" help:"set a limit for the number of concurrent connections (default: 5)"`
MaxRetries uint `option:"retries" help:"set the number of retries attempted"`
Region string `option:"region" help:"set region"`
2020-11-11 19:20:35 +00:00
BucketLookup string `option:"bucket-lookup" help:"bucket lookup style: 'auto', 'dns', or 'path'"`
ListObjectsV1 bool `option:"list-objects-v1" help:"use deprecated V1 api for ListObjects calls"`
2017-06-05 22:17:39 +00:00
}
// NewConfig returns a new Config with the default values filled in.
func NewConfig() Config {
return Config{
Connections: 5,
ListObjectsV1: false,
2017-06-05 22:17:39 +00:00
}
}
func init() {
options.Register("s3", Config{})
2015-12-28 14:51:24 +00:00
}
// ParseConfig parses the string s and extracts the s3 config. The two
// supported configuration formats are s3://host/bucketname/prefix and
2017-11-20 21:21:39 +00:00
// s3:host/bucketname/prefix. The host can also be a valid s3 region
// name. If no prefix is given the prefix "restic" will be used.
func ParseConfig(s string) (Config, error) {
2016-02-14 15:01:14 +00:00
switch {
case strings.HasPrefix(s, "s3:http"):
// assume that a URL has been specified, parse it and
// use the host as the endpoint and the path as the
// bucket name and prefix
url, err := url.Parse(s[3:])
2015-12-28 17:23:02 +00:00
if err != nil {
return Config{}, errors.WithStack(err)
2015-12-28 17:23:02 +00:00
}
if url.Path == "" {
return Config{}, errors.New("s3: bucket name not found")
2015-12-28 17:23:02 +00:00
}
bucket, path, _ := strings.Cut(url.Path[1:], "/")
return createConfig(url.Host, bucket, path, url.Scheme == "http")
case strings.HasPrefix(s, "s3://"):
s = s[5:]
2016-02-14 15:01:14 +00:00
case strings.HasPrefix(s, "s3:"):
s = s[3:]
2016-02-14 15:01:14 +00:00
default:
return Config{}, errors.New("s3: invalid format")
}
// use the first entry of the path as the endpoint and the
// remainder as bucket name and prefix
endpoint, rest, _ := strings.Cut(s, "/")
bucket, prefix, _ := strings.Cut(rest, "/")
return createConfig(endpoint, bucket, prefix, false)
}
func createConfig(endpoint, bucket, prefix string, useHTTP bool) (Config, error) {
if endpoint == "" {
return Config{}, errors.New("s3: invalid format, host/region or bucket name not found")
2017-11-20 21:29:15 +00:00
}
if prefix != "" {
prefix = path.Clean(prefix)
}
2017-11-20 21:29:15 +00:00
2017-06-05 22:17:39 +00:00
cfg := NewConfig()
cfg.Endpoint = endpoint
cfg.UseHTTP = useHTTP
cfg.Bucket = bucket
2017-06-05 22:17:39 +00:00
cfg.Prefix = prefix
return cfg, nil
2015-12-28 14:51:24 +00:00
}
// ApplyEnvironment saves values from the environment to the config.
func ApplyEnvironment(cfgRaw interface{}) error {
cfg := cfgRaw.(*Config)
if cfg.KeyID == "" {
cfg.KeyID = os.Getenv("AWS_ACCESS_KEY_ID")
}
if cfg.Secret.String() == "" {
cfg.Secret = options.NewSecretString(os.Getenv("AWS_SECRET_ACCESS_KEY"))
}
if cfg.KeyID == "" && cfg.Secret.String() != "" {
return errors.Fatalf("unable to open S3 backend: Key ID ($AWS_ACCESS_KEY_ID) is empty")
} else if cfg.KeyID != "" && cfg.Secret.String() == "" {
return errors.Fatalf("unable to open S3 backend: Secret ($AWS_SECRET_ACCESS_KEY) is empty")
}
if cfg.Region == "" {
cfg.Region = os.Getenv("AWS_DEFAULT_REGION")
}
return nil
}