-
Notifications
You must be signed in to change notification settings - Fork 10
/
config.go
109 lines (95 loc) · 2.3 KB
/
config.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"errors"
"log"
"github.com/minio/minio-go"
)
type Config struct {
s3_server string
s3_region string
s3_access_key string
s3_secret_key string
api_signature string
ssl bool
debug bool
}
type s3Client struct {
region string
s3Client *minio.Client
}
func (c *Config) NewClient() (interface{}, error) {
// Debug
if c.debug {
log.Printf("[DEBUG] Debug enabled.")
}
// S3 Server
if len(c.s3_server) < 1 {
log.Println("[FATAL] S3 Server undefined!")
return nil, errors.New("S3 Server not defined!")
}
if c.debug {
log.Printf("[DEBUG] S3 Server: [%s]", c.s3_server)
}
// S3 Region
if len(c.s3_region) < 1 {
if c.debug {
log.Println("S3 Region not defined. Using default value of [us-east-1]")
}
c.s3_region = "us-east-1"
}
if c.debug {
log.Printf("[DEBUG] S3 Region: [%s]", c.s3_region)
}
// S3 Access Key
if len(c.s3_access_key) < 1 {
log.Println("[FATAL] S3 Access Key not defined!")
return nil, errors.New("S3 Access Key not defined!")
}
if c.debug {
log.Printf("[DEBUG] S3 Access Key: [%s]", c.s3_access_key)
}
// S3 Secret Key
if len(c.s3_secret_key) < 1 {
log.Println("[FATAL] S3 Secret Key not defined!")
return nil, errors.New("S3 Secret Key not defined!")
}
if c.debug {
log.Printf("[DEBUG] S3 Secret Key: [%s]", c.s3_secret_key)
}
// API Signature
if len(c.api_signature) < 1 {
if c.debug {
log.Println("[DEBUG] API Signature not defined. Using default value of [v4]")
}
c.api_signature = "v4"
}
if c.debug {
log.Printf("[DEBUG] API Signature: [%s]", c.api_signature)
}
// SSL
if c.debug {
log.Printf("[DEBUG] SSL: %v", c.ssl)
}
// Initialize minio client object.
minioClient := new(minio.Client)
var err error
if c.api_signature == "v2" {
minioClient, err = minio.NewV2(c.s3_server, c.s3_access_key, c.s3_secret_key, c.ssl)
} else if c.api_signature == "v4" {
minioClient, err = minio.NewV4(c.s3_server, c.s3_access_key, c.s3_secret_key, c.ssl)
} else {
minioClient, err = minio.New(c.s3_server, c.s3_access_key, c.s3_secret_key, c.ssl)
}
if err != nil {
log.Println("[FATAL] Error connecting to S3 server.")
return nil, err
} else {
if c.debug {
log.Printf("[DEBUG] S3 client initialized")
}
}
return &s3Client{
region: c.s3_region,
s3Client: minioClient,
}, nil
}