Skip to content

Commit

Permalink
Adds better logs to the s3 store.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Leduc-Hamel committed Apr 6, 2021
1 parent 12b4b81 commit b44280d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/app/trieugene.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewTrieugeneStore(cfg *config.Config, kind string, key string, value string
}

func (t *trieugene) Run(ctx context.Context) error {
store := store.NewS3(&store.S3Params{
store := store.NewS3(t.cfg, &store.S3Params{
AccessKey: t.cfg.GCSAccessKey(),
SecretKey: t.cfg.GCSAccessSecret(),
URL: t.cfg.GCSURL(),
Expand All @@ -68,7 +68,7 @@ func (t *trieugeneDev) Run(ctx context.Context) error {
}

func (t *trieugeneStore) Run(ctx context.Context) error {
store := store.NewS3(&store.S3Params{
store := store.NewS3(t.cfg, &store.S3Params{
AccessKey: t.cfg.S3AccessKey(),
SecretKey: t.cfg.S3SecretKey(),
URL: t.cfg.S3URL(),
Expand Down
17 changes: 15 additions & 2 deletions pkg/store/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/mlhamel/trieugene/pkg/config"
)

type S3Params struct {
Expand All @@ -23,13 +24,14 @@ type S3Params struct {
}

type S3 struct {
cfg *config.Config
params *S3Params
client *s3.S3
}

const shortDuration = 100 * time.Millisecond

func NewS3(params *S3Params) Store {
func NewS3(cfg *config.Config, params *S3Params) Store {
conf := aws.Config{
Credentials: credentials.NewStaticCredentials(params.AccessKey, params.SecretKey, ""),
Endpoint: aws.String(params.URL),
Expand All @@ -40,10 +42,15 @@ func NewS3(params *S3Params) Store {

client := s3.New(session.New(&conf))

return &S3{client: client, params: params}
return &S3{
cfg: cfg,
client: client,
params: params,
}
}

func (s *S3) Setup(ctx context.Context) error {
s.cfg.Logger().Debug().Str("bucket", s.params.Bucket).Msg("Creating s3 bucket")
input := &s3.CreateBucketInput{Bucket: aws.String(s.params.Bucket)}
_, err := s.client.CreateBucket(input)
if err != nil {
Expand All @@ -53,24 +60,30 @@ func (s *S3) Setup(ctx context.Context) error {
case s3.ErrCodeBucketAlreadyOwnedByYou:
err = nil
default:
s.cfg.Logger().Error().Str("bucket", s.params.Bucket).Err(aerr).Msg("Failed: Creating s3 bucket")
return aerr
}
} else {
s.cfg.Logger().Error().Str("bucket", s.params.Bucket).Err(err).Msg("Failed: Creating s3 bucket and at extracting error")
return err
}
}
s.cfg.Logger().Debug().Str("bucket", s.params.Bucket).Msg("Succeed: Creating s3 bucket")
return nil
}

func (s *S3) Persist(ctx context.Context, filename string, data string) error {
s.cfg.Logger().Debug().Str("bucket", s.params.Bucket).Str("filename", filename).Msg("Persisting file using s3")
_, err := s.client.PutObjectWithContext(ctx, &s3.PutObjectInput{
Bucket: aws.String(s.params.Bucket),
Key: aws.String(filename),
Body: strings.NewReader(data),
})
if err != nil {
s.cfg.Logger().Error().Str("bucket", s.params.Bucket).Str("filename", filename).Err(err).Msg("Failed: Persisting file using s3")
return err
}

s.cfg.Logger().Debug().Str("bucket", s.params.Bucket).Str("filename", filename).Msg("Succeed: Persisting file using s3")
return nil
}
2 changes: 1 addition & 1 deletion services/rougecombien/pkg/apps/rougecombien.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Rougecombien struct {
}

func NewRougecombien(cfg *config.Config) *Rougecombien {
store := store.NewS3(&store.S3Params{
store := store.NewS3(cfg, &store.S3Params{
AccessKey: cfg.S3AccessKey(),
SecretKey: cfg.S3SecretKey(),
URL: cfg.S3URL(),
Expand Down

0 comments on commit b44280d

Please sign in to comment.