Skip to content

Commit e92d7e3

Browse files
committed
allow empty config for object storage
1 parent 7b43960 commit e92d7e3

File tree

1 file changed

+39
-49
lines changed

1 file changed

+39
-49
lines changed

pkg/storage/object/object.go

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"context"
2020
"io"
2121
"net/url"
22-
"reflect"
2322
"strings"
2423

2524
"github.com/minio/minio-go/v7"
@@ -47,11 +46,18 @@ type Config struct {
4746
Endpoint string
4847
AccessKeyID string
4948
SecretAccessKey string
50-
Bucket string
51-
BucketURL string
49+
client *minio.Client
50+
51+
Bucket string
52+
BucketURL string
53+
bucketURL *url.URL
5254
}
5355

5456
func (c *Config) Validate() error {
57+
if c.Empty() {
58+
return nil
59+
}
60+
5561
if c.Endpoint == "" {
5662
return errors.New("endpoint is required")
5763
}
@@ -64,12 +70,26 @@ func (c *Config) Validate() error {
6470
if c.SecretAccessKey == "" {
6571
return errors.New("secret access key is required")
6672
}
73+
client, err := minio.New(c.Endpoint, &minio.Options{
74+
Creds: credentials.NewStaticV4(c.AccessKeyID, c.SecretAccessKey, ""),
75+
Secure: true,
76+
})
77+
if err != nil {
78+
return errors.Wrap(err, "new minio client")
79+
}
80+
c.client = client
81+
6782
if c.Bucket == "" {
6883
return errors.New("bucket is required")
6984
}
7085
if c.BucketURL == "" {
7186
return errors.New("bucket url is required")
7287
}
88+
u, err := url.Parse(c.BucketURL)
89+
if err != nil {
90+
return errors.Wrap(err, "parse public url")
91+
}
92+
c.bucketURL = u
7393

7494
return nil
7595
}
@@ -86,6 +106,10 @@ func (c *Config) From(app *config.App) *Config {
86106
return c
87107
}
88108

109+
func (c *Config) Empty() bool {
110+
return c.Endpoint == "" && c.AccessKeyID == "" && c.SecretAccessKey == "" && c.Bucket == "" && c.BucketURL == ""
111+
}
112+
89113
type Dependencies struct{}
90114

91115
// --- Factory code block ---
@@ -113,59 +137,47 @@ func new(instance string, app *config.App, dependencies Dependencies) (Storage,
113137
return nil, errors.Wrap(err, "validate config")
114138
}
115139

116-
client, err := minio.New(config.Endpoint, &minio.Options{
117-
Creds: credentials.NewStaticV4(config.AccessKeyID, config.SecretAccessKey, ""),
118-
Secure: true,
119-
})
120-
if err != nil {
121-
return nil, errors.Wrap(err, "new minio client")
122-
}
123-
124-
u, err := url.Parse(config.BucketURL)
125-
if err != nil {
126-
return nil, errors.Wrap(err, "parse public url")
127-
}
128-
129140
return &s3{
130141
Base: component.New(&component.BaseConfig[Config, Dependencies]{
131142
Name: "ObjectStorage",
132143
Instance: instance,
133144
Config: config,
134145
Dependencies: dependencies,
135146
}),
136-
client: client,
137-
bucketURL: u,
138147
}, nil
139148
}
140149

141150
// --- Implementation code block ---
142151
type s3 struct {
143152
*component.Base[Config, Dependencies]
144-
145-
client *minio.Client
146-
bucketURL *url.URL
147153
}
148154

149155
func (s *s3) Put(ctx context.Context, key string, body io.Reader, contentType string) (publicURL string, err error) {
150156
ctx = telemetry.StartWith(ctx, append(s.TelemetryLabels(), telemetrymodel.KeyOperation, "Put")...)
151157
defer func() { telemetry.End(ctx, err) }()
152-
bucket := s.Config().Bucket
158+
config := s.Config()
159+
if config.Empty() {
160+
return "", errors.New("not configured")
161+
}
153162

154-
if _, err := s.client.PutObject(ctx, bucket, key, body, -1, minio.PutObjectOptions{
163+
if _, err := config.client.PutObject(ctx, config.Bucket, key, body, -1, minio.PutObjectOptions{
155164
ContentType: contentType,
156165
}); err != nil {
157166
return "", errors.Wrap(err, "put object")
158167
}
159168

160-
return s.bucketURL.JoinPath(key).String(), nil
169+
return config.bucketURL.JoinPath(key).String(), nil
161170
}
162171

163172
func (s *s3) Get(ctx context.Context, key string) (publicURL string, err error) {
164173
ctx = telemetry.StartWith(ctx, append(s.TelemetryLabels(), telemetrymodel.KeyOperation, "Get")...)
165174
defer func() { telemetry.End(ctx, err) }()
166-
bucket := s.Config().Bucket
175+
config := s.Config()
176+
if config.Empty() {
177+
return "", errors.New("not configured")
178+
}
167179

168-
if _, err := s.client.StatObject(ctx, bucket, key, minio.StatObjectOptions{}); err != nil {
180+
if _, err := config.client.StatObject(ctx, config.Bucket, key, minio.StatObjectOptions{}); err != nil {
169181
errResponse := minio.ToErrorResponse(err)
170182
if errResponse.Code == minio.NoSuchKey {
171183
return "", ErrNotFound
@@ -174,7 +186,7 @@ func (s *s3) Get(ctx context.Context, key string) (publicURL string, err error)
174186
return "", errors.Wrap(err, "stat object")
175187
}
176188

177-
return s.bucketURL.JoinPath(key).String(), nil
189+
return config.bucketURL.JoinPath(key).String(), nil
178190
}
179191

180192
func (s *s3) Reload(app *config.App) (err error) {
@@ -187,29 +199,7 @@ func (s *s3) Reload(app *config.App) (err error) {
187199
return errors.Wrap(err, "validate config")
188200
}
189201

190-
if reflect.DeepEqual(s.Config(), newConfig) {
191-
log.Debug(ctx, "object storage config not changed")
192-
193-
return nil
194-
}
195-
196-
client, err := minio.New(newConfig.Endpoint, &minio.Options{
197-
Creds: credentials.NewStaticV4(newConfig.AccessKeyID, newConfig.SecretAccessKey, ""),
198-
Secure: true,
199-
})
200-
if err != nil {
201-
return errors.Wrap(err, "new minio client")
202-
}
203-
204-
u, err := url.Parse(newConfig.BucketURL)
205-
if err != nil {
206-
return errors.Wrap(err, "parse public url")
207-
}
208-
209-
s.client = client
210-
s.bucketURL = u
211202
s.SetConfig(newConfig)
212-
213203
log.Info(ctx, "object storage reloaded")
214204

215205
return nil

0 commit comments

Comments
 (0)