From 5d602034eae993f3f58a2e5830e4222a17b9627f Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Fri, 12 May 2017 07:38:22 +0200 Subject: [PATCH] gateway: Use default params when no args provided (#4315) For S3 & Azure, use default parameters when no arguments (endpoint) are provided. This also avoids a crash. --- cmd/gateway-azure.go | 21 ++++++++++++++------- cmd/gateway-gcs.go | 22 +++++++--------------- cmd/gateway-main.go | 2 +- cmd/gateway-s3.go | 26 +++++++++++++++++--------- 4 files changed, 39 insertions(+), 32 deletions(-) diff --git a/cmd/gateway-azure.go b/cmd/gateway-azure.go index bb7a19349..f95b0c7d3 100644 --- a/cmd/gateway-azure.go +++ b/cmd/gateway-azure.go @@ -33,6 +33,7 @@ import ( "time" "github.com/Azure/azure-sdk-for-go/storage" + "github.com/minio/cli" "github.com/minio/minio-go/pkg/policy" "github.com/minio/sha256-simd" ) @@ -156,14 +157,20 @@ func azureToObjectError(err error, params ...string) error { } // Inits azure blob storage client and returns AzureObjects. -func newAzureLayer(args []string) (GatewayLayer, error) { - endPoint, secure, err := parseGatewayEndpoint(args[0]) - if err != nil { - return nil, err - } +func newAzureLayer(args cli.Args) (GatewayLayer, error) { - if endPoint == "" { - endPoint = storage.DefaultBaseURL + var err error + + // Default endpoint parameters + endPoint := storage.DefaultBaseURL + secure := true + + // If user provided some parameters + if len(args) > 0 { + endPoint, secure, err = parseGatewayEndpoint(args.First()) + if err != nil { + return nil, err + } } account := os.Getenv("MINIO_ACCESS_KEY") diff --git a/cmd/gateway-gcs.go b/cmd/gateway-gcs.go index 679593a5a..c5b7072df 100644 --- a/cmd/gateway-gcs.go +++ b/cmd/gateway-gcs.go @@ -17,32 +17,24 @@ package cmd import ( + "bytes" "context" "crypto/sha256" + "encoding/base64" + "encoding/hex" "errors" "fmt" "hash" "io" + "path" "strconv" "strings" - "encoding/hex" - "cloud.google.com/go/storage" - "google.golang.org/api/googleapi" "google.golang.org/api/iterator" - "encoding/base64" - - "bytes" - - // this package contains the url code of go 1.8 - // due to compatibility with older versions - // copied it to our rep - - "path" - + "github.com/minio/cli" minio "github.com/minio/minio-go" "github.com/minio/minio-go/pkg/policy" ) @@ -171,7 +163,7 @@ type gcsGateway struct { } // newGCSGateway returns gcs gatewaylayer -func newGCSGateway(args []string) (GatewayLayer, error) { +func newGCSGateway(args cli.Args) (GatewayLayer, error) { if len(args) != 1 { return nil, fmt.Errorf("ProjectID expected") } @@ -183,7 +175,7 @@ func newGCSGateway(args []string) (GatewayLayer, error) { endpoint := "storage.googleapis.com" secure := true - projectID := args[0] + projectID := args.First() ctx := context.Background() diff --git a/cmd/gateway-main.go b/cmd/gateway-main.go index b4b9333eb..c08d8108d 100644 --- a/cmd/gateway-main.go +++ b/cmd/gateway-main.go @@ -179,7 +179,7 @@ const ( ) // GatewayFn returns the GatewayLayer for the backend -type GatewayFn func([]string) (GatewayLayer, error) +type GatewayFn func(cli.Args) (GatewayLayer, error) var ( backends = map[gatewayBackend]GatewayFn{ diff --git a/cmd/gateway-s3.go b/cmd/gateway-s3.go index dde2ddbc2..3d7b24a9f 100644 --- a/cmd/gateway-s3.go +++ b/cmd/gateway-s3.go @@ -17,13 +17,15 @@ package cmd import ( - "encoding/hex" "errors" "io" "net/http" "os" "path" + "encoding/hex" + + "github.com/minio/cli" minio "github.com/minio/minio-go" "github.com/minio/minio-go/pkg/policy" ) @@ -99,15 +101,21 @@ type s3Objects struct { } // newS3Gateway returns s3 gatewaylayer -func newS3Gateway(args []string) (GatewayLayer, error) { - endpoint, secure, err := parseGatewayEndpoint(args[0]) - if err != nil { - return nil, err - } +func newS3Gateway(args cli.Args) (GatewayLayer, error) { - if endpoint == "" { - endpoint = "s3.amazonaws.com" - secure = true + var err error + + // Default endpoint parameters + endpoint := "s3.amazonaws.com" + secure := true + + // Check if user provided some parameters + if len(args) > 0 { + // Override default params if the endpoint is provided + endpoint, secure, err = parseGatewayEndpoint(args.First()) + if err != nil { + return nil, err + } } accessKey := os.Getenv("MINIO_ACCESS_KEY")