Skip to content

Commit

Permalink
Merge pull request #27 from fujiwara/refactor-s3
Browse files Browse the repository at this point in the history
create a s3 client once.
  • Loading branch information
fujiwara authored Mar 10, 2019
2 parents 8b58f82 + 2a4638b commit 13e6da0
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions stretcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
var (
LogBuffer bytes.Buffer
Version string
s3svc *s3.S3
)

type Config struct {
Expand Down Expand Up @@ -94,13 +95,10 @@ func Run(conf Config) error {
}

func getS3(u *url.URL) (io.ReadCloser, error) {
sess, err := session.NewSession()
svc, err := initS3()
if err != nil {
return nil, err
}
log.Println("region:", *sess.Config.Region)

svc := s3.New(sess)
key := strings.TrimLeft(u.Path, "/")
result, err := svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(u.Host),
Expand Down Expand Up @@ -198,3 +196,15 @@ func parseEvents() (string, error) {
return strings.Trim(line, "\n"), err
}
}

func initS3() (*s3.S3, error) {
if s3svc != nil {
return s3svc, nil
}
sess, err := session.NewSession()
if err != nil {
return nil, err
}
log.Println("region:", *sess.Config.Region)
return s3.New(sess), nil
}

0 comments on commit 13e6da0

Please sign in to comment.