diff --git a/awss3/store.go b/awss3/store.go index dee97c5..229ffb8 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 @@ -107,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). @@ -125,34 +144,38 @@ 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 != "" { 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) } - 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