Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure S3 bucket #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions awss3/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sergeyt shouldn't you be checking the error returned from this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@epsniff yes, but only network connections I guess. if bucket exists should be no error

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ensureBucket(s3Client, conf.Bucket)
err = ensureBucket(s3Client, conf.Bucket)
if err != nil {
return nil, nil, err
}


return s3Client, sess, nil
}

func makeAWSConf(conf *cloudstorage.Config) (*aws.Config, error) {
awsConf := aws.NewConfig().
WithHTTPClient(http.DefaultClient).
WithMaxRetries(aws.UseServiceDefaultRetries).
Expand All @@ -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
Expand Down