Consolidate the s3 driver parameters with a struct

This commit is contained in:
Andrey Kostov 2015-01-07 12:18:42 +02:00
parent 7c9112fc3c
commit bc9509d85f
2 changed files with 48 additions and 12 deletions

View file

@ -39,6 +39,18 @@ const chunkSize = 5 * 1024 * 1024
// listMax is the largest amount of objects you can request from S3 in a list call // listMax is the largest amount of objects you can request from S3 in a list call
const listMax = 1000 const listMax = 1000
//DriverParameters A struct that encapsulates all of the driver parameters after all values have been set
type DriverParameters struct {
AccessKey string
SecretKey string
Bucket string
Region aws.Region
Encrypt bool
Secure bool
V4Auth bool
RootDirectory string
}
func init() { func init() {
factory.Register(driverName, &s3DriverFactory{}) factory.Register(driverName, &s3DriverFactory{})
} }
@ -119,28 +131,39 @@ func FromParameters(parameters map[string]interface{}) (*Driver, error) {
rootDirectory = "" rootDirectory = ""
} }
return New(fmt.Sprint(accessKey), fmt.Sprint(secretKey), fmt.Sprint(bucket), fmt.Sprint(rootDirectory), region, encryptBool, secureBool, v4AuthBool) params := DriverParameters{
fmt.Sprint(accessKey),
fmt.Sprint(secretKey),
fmt.Sprint(bucket),
region,
encryptBool,
secureBool,
v4AuthBool,
fmt.Sprint(rootDirectory),
}
return New(params)
} }
// New constructs a new Driver with the given AWS credentials, region, encryption flag, and // New constructs a new Driver with the given AWS credentials, region, encryption flag, and
// bucketName // bucketName
func New(accessKey, secretKey, bucketName, rootDirectory string, region aws.Region, encrypt, secure, v4auth bool) (*Driver, error) { func New(params DriverParameters) (*Driver, error) {
auth, err := aws.GetAuth(accessKey, secretKey, "", time.Time{}) auth, err := aws.GetAuth(params.AccessKey, params.SecretKey, "", time.Time{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
if !secure { if !params.Secure {
region.S3Endpoint = strings.Replace(region.S3Endpoint, "https", "http", 1) params.Region.S3Endpoint = strings.Replace(params.Region.S3Endpoint, "https", "http", 1)
} }
s3obj := s3.New(auth, region) s3obj := s3.New(auth, params.Region)
bucket := s3obj.Bucket(bucketName) bucket := s3obj.Bucket(params.Bucket)
if v4auth { if params.V4Auth {
s3obj.Signature = aws.V4Signature s3obj.Signature = aws.V4Signature
} else { } else {
if region.Name == "eu-central-1" { if params.Region.Name == "eu-central-1" {
return nil, fmt.Errorf("The eu-central-1 region only works with v4 authentication") return nil, fmt.Errorf("The eu-central-1 region only works with v4 authentication")
} }
} }
@ -164,7 +187,7 @@ func New(accessKey, secretKey, bucketName, rootDirectory string, region aws.Regi
// } // }
// } // }
return &Driver{s3obj, bucket, encrypt, rootDirectory}, nil return &Driver{s3obj, bucket, params.Encrypt, params.RootDirectory}, nil
} }
// Implement the storagedriver.StorageDriver interface // Implement the storagedriver.StorageDriver interface

View file

@ -28,9 +28,10 @@ func init() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer os.Remove(root)
s3DriverConstructor := func(region aws.Region) (storagedriver.StorageDriver, error) { s3DriverConstructor := func(region aws.Region) (storagedriver.StorageDriver, error) {
encryptBool := true encryptBool := false
if encrypt != "" { if encrypt != "" {
encryptBool, err = strconv.ParseBool(encrypt) encryptBool, err = strconv.ParseBool(encrypt)
if err != nil { if err != nil {
@ -53,7 +54,19 @@ func init() {
return nil, err return nil, err
} }
} }
return New(accessKey, secretKey, bucket, root, region, encryptBool, secureBool, v4AuthBool)
parameters := DriverParameters{
accessKey,
secretKey,
bucket,
region,
encryptBool,
secureBool,
v4AuthBool,
root,
}
return New(parameters)
} }
// Skip S3 storage driver tests if environment variable parameters are not provided // Skip S3 storage driver tests if environment variable parameters are not provided