-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
72 lines (63 loc) · 1.83 KB
/
client.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
package minio
import (
"context"
"log"
"github.com/OJ-lab/oj-lab-services/core"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
const (
minioEndpointProp = "minio.endpoint"
minioAccessKeyProp = "minio.accessKeyID"
minioSecretAccessKeyProp = "minio.secretAccessKey"
minioUseSSLProp = "minio.useSSL"
minioRegionProp = "minio.region"
minioBucketNameProp = "minio.bucketName"
)
var (
endpoint string
accessKeyID string
secretAccessKey string
useSSL bool
region string
minioClient *minio.Client
bucketName string
)
func init() {
endpoint = core.AppConfig.GetString(minioEndpointProp)
accessKeyID = core.AppConfig.GetString(minioAccessKeyProp)
secretAccessKey = core.AppConfig.GetString(minioSecretAccessKeyProp)
useSSL = core.AppConfig.GetBool(minioUseSSLProp)
region = core.AppConfig.GetString(minioRegionProp)
bucketName = core.AppConfig.GetString(minioBucketNameProp)
}
func GetBucketName() string {
return bucketName
}
func GetMinioClient() *minio.Client {
if minioClient == nil {
var err error
minioClient, err = minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
Region: region,
})
if err != nil {
panic("failed to connect minio client")
}
ctx := context.Background()
err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{})
if err != nil {
// Check to see if we already own this bucket (which happens if you run this twice)
exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
if errBucketExists == nil && exists {
log.Printf("We already own %s\n", bucketName)
} else {
log.Fatalln(err)
}
} else {
log.Printf("Successfully created %s\n", bucketName)
}
}
return minioClient
}