@@ -19,7 +19,6 @@ import (
19
19
"context"
20
20
"io"
21
21
"net/url"
22
- "reflect"
23
22
"strings"
24
23
25
24
"github.com/minio/minio-go/v7"
@@ -47,11 +46,18 @@ type Config struct {
47
46
Endpoint string
48
47
AccessKeyID string
49
48
SecretAccessKey string
50
- Bucket string
51
- BucketURL string
49
+ client * minio.Client
50
+
51
+ Bucket string
52
+ BucketURL string
53
+ bucketURL * url.URL
52
54
}
53
55
54
56
func (c * Config ) Validate () error {
57
+ if c .Empty () {
58
+ return nil
59
+ }
60
+
55
61
if c .Endpoint == "" {
56
62
return errors .New ("endpoint is required" )
57
63
}
@@ -64,12 +70,26 @@ func (c *Config) Validate() error {
64
70
if c .SecretAccessKey == "" {
65
71
return errors .New ("secret access key is required" )
66
72
}
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
+
67
82
if c .Bucket == "" {
68
83
return errors .New ("bucket is required" )
69
84
}
70
85
if c .BucketURL == "" {
71
86
return errors .New ("bucket url is required" )
72
87
}
88
+ u , err := url .Parse (c .BucketURL )
89
+ if err != nil {
90
+ return errors .Wrap (err , "parse public url" )
91
+ }
92
+ c .bucketURL = u
73
93
74
94
return nil
75
95
}
@@ -86,6 +106,10 @@ func (c *Config) From(app *config.App) *Config {
86
106
return c
87
107
}
88
108
109
+ func (c * Config ) Empty () bool {
110
+ return c .Endpoint == "" && c .AccessKeyID == "" && c .SecretAccessKey == "" && c .Bucket == "" && c .BucketURL == ""
111
+ }
112
+
89
113
type Dependencies struct {}
90
114
91
115
// --- Factory code block ---
@@ -113,59 +137,47 @@ func new(instance string, app *config.App, dependencies Dependencies) (Storage,
113
137
return nil , errors .Wrap (err , "validate config" )
114
138
}
115
139
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
-
129
140
return & s3 {
130
141
Base : component .New (& component.BaseConfig [Config , Dependencies ]{
131
142
Name : "ObjectStorage" ,
132
143
Instance : instance ,
133
144
Config : config ,
134
145
Dependencies : dependencies ,
135
146
}),
136
- client : client ,
137
- bucketURL : u ,
138
147
}, nil
139
148
}
140
149
141
150
// --- Implementation code block ---
142
151
type s3 struct {
143
152
* component.Base [Config , Dependencies ]
144
-
145
- client * minio.Client
146
- bucketURL * url.URL
147
153
}
148
154
149
155
func (s * s3 ) Put (ctx context.Context , key string , body io.Reader , contentType string ) (publicURL string , err error ) {
150
156
ctx = telemetry .StartWith (ctx , append (s .TelemetryLabels (), telemetrymodel .KeyOperation , "Put" )... )
151
157
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
+ }
153
162
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 {
155
164
ContentType : contentType ,
156
165
}); err != nil {
157
166
return "" , errors .Wrap (err , "put object" )
158
167
}
159
168
160
- return s .bucketURL .JoinPath (key ).String (), nil
169
+ return config .bucketURL .JoinPath (key ).String (), nil
161
170
}
162
171
163
172
func (s * s3 ) Get (ctx context.Context , key string ) (publicURL string , err error ) {
164
173
ctx = telemetry .StartWith (ctx , append (s .TelemetryLabels (), telemetrymodel .KeyOperation , "Get" )... )
165
174
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
+ }
167
179
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 {
169
181
errResponse := minio .ToErrorResponse (err )
170
182
if errResponse .Code == minio .NoSuchKey {
171
183
return "" , ErrNotFound
@@ -174,7 +186,7 @@ func (s *s3) Get(ctx context.Context, key string) (publicURL string, err error)
174
186
return "" , errors .Wrap (err , "stat object" )
175
187
}
176
188
177
- return s .bucketURL .JoinPath (key ).String (), nil
189
+ return config .bucketURL .JoinPath (key ).String (), nil
178
190
}
179
191
180
192
func (s * s3 ) Reload (app * config.App ) (err error ) {
@@ -187,29 +199,7 @@ func (s *s3) Reload(app *config.App) (err error) {
187
199
return errors .Wrap (err , "validate config" )
188
200
}
189
201
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
211
202
s .SetConfig (newConfig )
212
-
213
203
log .Info (ctx , "object storage reloaded" )
214
204
215
205
return nil
0 commit comments