Keepers d983a77a57
normalize s3 bucket names (#847)
## Description

Allow users to provide bucket names prefixed
with the s3 scheme: s3://my-bucket

## Type of change

- [x] 🐹 Trivial/Minor

## Issue(s)

* #560

## Test Plan

- [ ] 💪 Manual
- [x]  Unit test
- [ ] 💚 E2E
2022-09-19 19:04:32 +00:00

76 lines
1.5 KiB
Go

package storage
import (
"strings"
"github.com/pkg/errors"
)
type S3Config struct {
Bucket string // required
Endpoint string
Prefix string
}
// config key consts
const (
keyS3Bucket = "s3_bucket"
keyS3Endpoint = "s3_endpoint"
keyS3Prefix = "s3_prefix"
)
// config exported name consts
const (
Bucket = "bucket"
Endpoint = "endpoint"
Prefix = "prefix"
)
func (c S3Config) Normalize() S3Config {
return S3Config{
Bucket: strings.TrimPrefix(c.Bucket, "s3://"),
Endpoint: c.Endpoint,
Prefix: c.Prefix,
}
}
// StringConfig transforms a s3Config struct into a plain
// map[string]string. All values in the original struct which
// serialize into the map are expected to be strings.
func (c S3Config) StringConfig() (map[string]string, error) {
cn := c.Normalize()
cfg := map[string]string{
keyS3Bucket: cn.Bucket,
keyS3Endpoint: cn.Endpoint,
keyS3Prefix: cn.Prefix,
}
return cfg, c.validate()
}
// S3Config retrieves the S3Config details from the Storage config.
func (s Storage) S3Config() (S3Config, error) {
c := S3Config{}
if len(s.Config) > 0 {
c.Bucket = orEmptyString(s.Config[keyS3Bucket])
c.Endpoint = orEmptyString(s.Config[keyS3Endpoint])
c.Prefix = orEmptyString(s.Config[keyS3Prefix])
}
return c, c.validate()
}
func (c S3Config) validate() error {
check := map[string]string{
Bucket: c.Bucket,
}
for k, v := range check {
if len(v) == 0 {
return errors.Wrap(errMissingRequired, k)
}
}
return nil
}