-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
52 lines (44 loc) · 1.19 KB
/
s3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package bucket
import (
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"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/leonardonicola/tickethub/config"
)
var (
s3Client *s3.S3
logger *config.Logger
)
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
func InitS3Client() {
logger = config.NewLogger()
access_key, found := os.LookupEnv("AWS_ACCESS_KEY")
if !found {
logger.Fatalf("AWS S3 - access key not found")
exitErrorf("AWS S3 - access key not found")
}
secret_key, found := os.LookupEnv("AWS_SECRET_ACCESS_KEY")
if !found {
logger.Fatalf("AWS S3 - secret access key not found")
exitErrorf("AWS S3 - secret access key not found")
}
s3Session, err := session.NewSession(&aws.Config{
Region: aws.String("sa-east-1"),
Credentials: credentials.NewStaticCredentials(access_key, secret_key, "")},
)
if err != nil {
logger.Errorf("AWS S3 - error occurred: %v", err)
exitErrorf("AWS S3 - error occurred: %v", err)
}
logger.Debug("Created new S3 client")
s3Client = s3.New(s3Session)
}
func GetS3Client() *s3.S3 {
return s3Client
}