-
Notifications
You must be signed in to change notification settings - Fork 7
/
binah.go
123 lines (110 loc) · 2.76 KB
/
binah.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package data
import (
"context"
"io"
"net/url"
"time"
"github.com/tuihub/librarian/app/sephirah/internal/biz/bizbinah"
"github.com/tuihub/librarian/internal/conf"
"github.com/tuihub/librarian/internal/lib/logger"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
type binahRepo struct {
mc *minio.Client
buckets map[bizbinah.Bucket]string
}
func NewBinahRepo(c *conf.S3) (bizbinah.BinahRepo, error) {
if c == nil {
return new(binahRepo), nil
}
minioClient, err := minio.New(c.GetEndPoint(), &minio.Options{ //nolint:exhaustruct //TODO
Creds: credentials.NewStaticV4(c.GetAccessKey(), c.GetSecretKey(), ""),
Secure: c.GetUseSsl(),
})
if err != nil {
return nil, err
}
bucketName := defaultBucketName()
location := "us-east-1"
for i, v := range bucketName {
if i == bizbinah.BucketUnspecified {
continue
}
if err = initBucket(minioClient, v, location); err != nil {
return nil, err
}
}
return &binahRepo{
mc: minioClient,
buckets: bucketName,
}, nil
}
func initBucket(mc *minio.Client, bucketName, location string) error {
err := mc.MakeBucket(
context.Background(),
bucketName,
minio.MakeBucketOptions{
Region: location,
ObjectLocking: false,
},
)
if err != nil {
// Check to see if we already own this bucket (which happens if you run this twice)
exists, errBucketExists := mc.BucketExists(context.Background(), bucketName)
if errBucketExists == nil && exists {
logger.Infof("We already own %s\n", bucketName)
} else {
logger.Error(err)
return err
}
} else {
logger.Infof("Successfully created %s\n", bucketName)
}
return nil
}
func defaultBucketName() map[bizbinah.Bucket]string {
return map[bizbinah.Bucket]string{
bizbinah.BucketUnspecified: "",
bizbinah.BucketDefault: "default",
}
}
func (s *binahRepo) FeatureEnabled() bool {
return s.mc != nil
}
func (s *binahRepo) PutObject(ctx context.Context, r io.Reader, bucket bizbinah.Bucket, objectName string) error {
_, err := s.mc.PutObject(
ctx,
s.buckets[bucket],
objectName,
r,
-1,
minio.PutObjectOptions{}, //nolint:exhaustruct // default value
)
return err
}
func (s *binahRepo) PresignedPutObject(
ctx context.Context,
bucket bizbinah.Bucket,
objectName string,
expires time.Duration,
) (string, error) {
res, err := s.mc.PresignedPutObject(ctx, s.buckets[bucket], objectName, expires)
if err != nil {
return "", err
}
return res.String(), nil
}
func (s *binahRepo) PresignedGetObject(
ctx context.Context,
bucket bizbinah.Bucket,
objectName string,
expires time.Duration,
) (string, error) {
reqParams := make(url.Values)
res, err := s.mc.PresignedGetObject(ctx, s.buckets[bucket], objectName, expires, reqParams)
if err != nil {
return "", err
}
return res.String(), nil
}