From e1d0ed02e2d9a84d3dd73da6351de03176fd99db Mon Sep 17 00:00:00 2001 From: Sergey Todyshev Date: Mon, 3 Feb 2020 00:08:44 +0300 Subject: [PATCH 1/2] config to enable debug log level --- awss3/store.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/awss3/store.go b/awss3/store.go index dee97c5..330d4f5 100644 --- a/awss3/store.go +++ b/awss3/store.go @@ -40,6 +40,8 @@ const ( ConfKeyARN = "arn" // ConfKeyDisableSSL config key name of disabling ssl flag ConfKeyDisableSSL = "disable_ssl" + // ConfKeyDebugLog config key to enable LogDebug log level + ConfKeyDebugLog = "debug_log" // Authentication Source's // AuthAccessKey is for using aws access key/secret pairs @@ -140,6 +142,10 @@ func NewClient(conf *cloudstorage.Config) (*s3.S3, *session.Session, error) { awsConf.WithEndpoint(conf.BaseUrl).WithS3ForcePathStyle(true) } + if conf.Settings.Bool(ConfKeyDebugLog) { + awsConf.WithLogLevel(aws.LogDebug) + } + disableSSL := conf.Settings.Bool(ConfKeyDisableSSL) if disableSSL { awsConf.WithDisableSSL(true) From 0bc159d62bcf5bb9cf54b531d5ad540b955b5135 Mon Sep 17 00:00:00 2001 From: Sergey Todyshev Date: Sun, 9 Feb 2020 01:22:18 +0300 Subject: [PATCH 2/2] ensure s3 bucket --- awss3/store.go | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/awss3/store.go b/awss3/store.go index 330d4f5..229ffb8 100644 --- a/awss3/store.go +++ b/awss3/store.go @@ -109,7 +109,24 @@ type ( // NewClient create new AWS s3 Client. Uses cloudstorage.Config to read // necessary config settings such as bucket, region, auth. func NewClient(conf *cloudstorage.Config) (*s3.S3, *session.Session, error) { + awsConf, err := makeAWSConf(conf) + if err != nil { + return nil, nil, err + } + + sess := session.New(awsConf) + if sess == nil { + return nil, nil, ErrNoS3Session + } + + s3Client := s3.New(sess) + ensureBucket(s3Client, conf.Bucket) + + return s3Client, sess, nil +} + +func makeAWSConf(conf *cloudstorage.Config) (*aws.Config, error) { awsConf := aws.NewConfig(). WithHTTPClient(http.DefaultClient). WithMaxRetries(aws.UseServiceDefaultRetries). @@ -127,15 +144,15 @@ func NewClient(conf *cloudstorage.Config) (*s3.S3, *session.Session, error) { case AuthAccessKey: accessKey := conf.Settings.String(ConfKeyAccessKey) if accessKey == "" { - return nil, nil, ErrNoAccessKey + return nil, ErrNoAccessKey } secretKey := conf.Settings.String(ConfKeyAccessSecret) if secretKey == "" { - return nil, nil, ErrNoAccessSecret + return nil, ErrNoAccessSecret } awsConf.WithCredentials(credentials.NewStaticCredentials(accessKey, secretKey, "")) default: - return nil, nil, ErrNoAuth + return nil, ErrNoAuth } if conf.BaseUrl != "" { @@ -151,14 +168,14 @@ func NewClient(conf *cloudstorage.Config) (*s3.S3, *session.Session, error) { awsConf.WithDisableSSL(true) } - sess := session.New(awsConf) - if sess == nil { - return nil, nil, ErrNoS3Session - } - - s3Client := s3.New(sess) + return awsConf, nil +} - return s3Client, sess, nil +func ensureBucket(s3Client *s3.S3, bucket string) error { + _, err := s3Client.CreateBucket(&s3.CreateBucketInput{ + Bucket: aws.String(bucket), + }) + return err } // NewStore Create AWS S3 storage client of type cloudstorage.Store