From 0e452f4dc793b747cdc2bcc8cff4dfdbddeb0028 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 4 Feb 2021 10:36:59 +0300 Subject: [PATCH] rearrange init code Signed-off-by: Vasiliy Tolstov --- s3.go | 46 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/s3.go b/s3.go index 3d55f21..28643c3 100644 --- a/s3.go +++ b/s3.go @@ -81,6 +81,27 @@ func (s *s3Store) Init(opts ...store.Option) error { } if v, ok := s.opts.Context.Value(endpointKey{}).(string); ok && v != "" { endpoint = v + var secure bool + + if strings.HasPrefix(endpoint, "https://") || s.opts.TLSConfig != nil { + secure = true + } + + if u, err := url.Parse(endpoint); err == nil && u.Host != "" { + endpoint = u.Host + } + + ts, err := minio.DefaultTransport(secure) + if err != nil { + return fmt.Errorf("init error: %w", err) + } + if s.opts.TLSConfig != nil { + ts.TLSClientConfig = s.opts.TLSConfig + } + + s.mopts.Transport = ts + s.mopts.Secure = secure + s.endpoint = endpoint } if v, ok := s.opts.Context.Value(regionKey{}).(string); ok && v != "" { region = v @@ -92,33 +113,8 @@ func (s *s3Store) Init(opts ...store.Option) error { s.mopts.Creds = creds.NewStaticV2(akey, skey, "") } - if len(endpoint) == 0 { - return fmt.Errorf("missing Endpoint option") - } - - var secure bool - - if strings.HasPrefix(endpoint, "https://") || s.opts.TLSConfig != nil { - secure = true - } - - if u, err := url.Parse(endpoint); err == nil && u.Host != "" { - endpoint = u.Host - } - - ts, err := minio.DefaultTransport(secure) - if err != nil { - return fmt.Errorf("init error: %w", err) - } - - if s.opts.TLSConfig != nil { - ts.TLSClientConfig = s.opts.TLSConfig - } - s.mopts.Transport = ts s.mopts.Region = region - s.endpoint = endpoint - return nil }