Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e92d7e322e | ||
|
|
7b4396067b |
6
.github/workflows/ci.yml
vendored
6
.github/workflows/ci.yml
vendored
@@ -5,6 +5,8 @@ on:
|
|||||||
branches: [ main, dev ]
|
branches: [ main, dev ]
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [ main ]
|
branches: [ main ]
|
||||||
|
release:
|
||||||
|
types: [ published ]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
@@ -25,7 +27,7 @@ jobs:
|
|||||||
build-and-push:
|
build-and-push:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: test
|
needs: test
|
||||||
if: github.event_name == 'push'
|
if: github.event_name == 'release' || (github.event_name == 'push' && github.ref_name == 'dev')
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
@@ -36,7 +38,7 @@ jobs:
|
|||||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||||
- name: Build and push Docker image (main)
|
- name: Build and push Docker image (main)
|
||||||
if: github.ref_name == 'main'
|
if: github.event_name == 'release'
|
||||||
run: make push
|
run: make push
|
||||||
- name: Build and push Docker image (dev)
|
- name: Build and push Docker image (dev)
|
||||||
if: github.ref_name == 'dev'
|
if: github.ref_name == 'dev'
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/minio/minio-go/v7"
|
"github.com/minio/minio-go/v7"
|
||||||
@@ -47,11 +46,18 @@ type Config struct {
|
|||||||
Endpoint string
|
Endpoint string
|
||||||
AccessKeyID string
|
AccessKeyID string
|
||||||
SecretAccessKey string
|
SecretAccessKey string
|
||||||
Bucket string
|
client *minio.Client
|
||||||
BucketURL string
|
|
||||||
|
Bucket string
|
||||||
|
BucketURL string
|
||||||
|
bucketURL *url.URL
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) Validate() error {
|
func (c *Config) Validate() error {
|
||||||
|
if c.Empty() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if c.Endpoint == "" {
|
if c.Endpoint == "" {
|
||||||
return errors.New("endpoint is required")
|
return errors.New("endpoint is required")
|
||||||
}
|
}
|
||||||
@@ -64,12 +70,26 @@ func (c *Config) Validate() error {
|
|||||||
if c.SecretAccessKey == "" {
|
if c.SecretAccessKey == "" {
|
||||||
return errors.New("secret access key is required")
|
return errors.New("secret access key is required")
|
||||||
}
|
}
|
||||||
|
client, err := minio.New(c.Endpoint, &minio.Options{
|
||||||
|
Creds: credentials.NewStaticV4(c.AccessKeyID, c.SecretAccessKey, ""),
|
||||||
|
Secure: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "new minio client")
|
||||||
|
}
|
||||||
|
c.client = client
|
||||||
|
|
||||||
if c.Bucket == "" {
|
if c.Bucket == "" {
|
||||||
return errors.New("bucket is required")
|
return errors.New("bucket is required")
|
||||||
}
|
}
|
||||||
if c.BucketURL == "" {
|
if c.BucketURL == "" {
|
||||||
return errors.New("bucket url is required")
|
return errors.New("bucket url is required")
|
||||||
}
|
}
|
||||||
|
u, err := url.Parse(c.BucketURL)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "parse public url")
|
||||||
|
}
|
||||||
|
c.bucketURL = u
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -86,6 +106,10 @@ func (c *Config) From(app *config.App) *Config {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Config) Empty() bool {
|
||||||
|
return c.Endpoint == "" && c.AccessKeyID == "" && c.SecretAccessKey == "" && c.Bucket == "" && c.BucketURL == ""
|
||||||
|
}
|
||||||
|
|
||||||
type Dependencies struct{}
|
type Dependencies struct{}
|
||||||
|
|
||||||
// --- Factory code block ---
|
// --- Factory code block ---
|
||||||
@@ -113,19 +137,6 @@ func new(instance string, app *config.App, dependencies Dependencies) (Storage,
|
|||||||
return nil, errors.Wrap(err, "validate config")
|
return nil, errors.Wrap(err, "validate config")
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := minio.New(config.Endpoint, &minio.Options{
|
|
||||||
Creds: credentials.NewStaticV4(config.AccessKeyID, config.SecretAccessKey, ""),
|
|
||||||
Secure: true,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "new minio client")
|
|
||||||
}
|
|
||||||
|
|
||||||
u, err := url.Parse(config.BucketURL)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "parse public url")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &s3{
|
return &s3{
|
||||||
Base: component.New(&component.BaseConfig[Config, Dependencies]{
|
Base: component.New(&component.BaseConfig[Config, Dependencies]{
|
||||||
Name: "ObjectStorage",
|
Name: "ObjectStorage",
|
||||||
@@ -133,39 +144,40 @@ func new(instance string, app *config.App, dependencies Dependencies) (Storage,
|
|||||||
Config: config,
|
Config: config,
|
||||||
Dependencies: dependencies,
|
Dependencies: dependencies,
|
||||||
}),
|
}),
|
||||||
client: client,
|
|
||||||
bucketURL: u,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Implementation code block ---
|
// --- Implementation code block ---
|
||||||
type s3 struct {
|
type s3 struct {
|
||||||
*component.Base[Config, Dependencies]
|
*component.Base[Config, Dependencies]
|
||||||
|
|
||||||
client *minio.Client
|
|
||||||
bucketURL *url.URL
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *s3) Put(ctx context.Context, key string, body io.Reader, contentType string) (publicURL string, err error) {
|
func (s *s3) Put(ctx context.Context, key string, body io.Reader, contentType string) (publicURL string, err error) {
|
||||||
ctx = telemetry.StartWith(ctx, append(s.TelemetryLabels(), telemetrymodel.KeyOperation, "Put")...)
|
ctx = telemetry.StartWith(ctx, append(s.TelemetryLabels(), telemetrymodel.KeyOperation, "Put")...)
|
||||||
defer func() { telemetry.End(ctx, err) }()
|
defer func() { telemetry.End(ctx, err) }()
|
||||||
bucket := s.Config().Bucket
|
config := s.Config()
|
||||||
|
if config.Empty() {
|
||||||
|
return "", errors.New("not configured")
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := s.client.PutObject(ctx, bucket, key, body, -1, minio.PutObjectOptions{
|
if _, err := config.client.PutObject(ctx, config.Bucket, key, body, -1, minio.PutObjectOptions{
|
||||||
ContentType: contentType,
|
ContentType: contentType,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return "", errors.Wrap(err, "put object")
|
return "", errors.Wrap(err, "put object")
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.bucketURL.JoinPath(key).String(), nil
|
return config.bucketURL.JoinPath(key).String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *s3) Get(ctx context.Context, key string) (publicURL string, err error) {
|
func (s *s3) Get(ctx context.Context, key string) (publicURL string, err error) {
|
||||||
ctx = telemetry.StartWith(ctx, append(s.TelemetryLabels(), telemetrymodel.KeyOperation, "Get")...)
|
ctx = telemetry.StartWith(ctx, append(s.TelemetryLabels(), telemetrymodel.KeyOperation, "Get")...)
|
||||||
defer func() { telemetry.End(ctx, err) }()
|
defer func() { telemetry.End(ctx, err) }()
|
||||||
bucket := s.Config().Bucket
|
config := s.Config()
|
||||||
|
if config.Empty() {
|
||||||
|
return "", errors.New("not configured")
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := s.client.StatObject(ctx, bucket, key, minio.StatObjectOptions{}); err != nil {
|
if _, err := config.client.StatObject(ctx, config.Bucket, key, minio.StatObjectOptions{}); err != nil {
|
||||||
errResponse := minio.ToErrorResponse(err)
|
errResponse := minio.ToErrorResponse(err)
|
||||||
if errResponse.Code == minio.NoSuchKey {
|
if errResponse.Code == minio.NoSuchKey {
|
||||||
return "", ErrNotFound
|
return "", ErrNotFound
|
||||||
@@ -174,7 +186,7 @@ func (s *s3) Get(ctx context.Context, key string) (publicURL string, err error)
|
|||||||
return "", errors.Wrap(err, "stat object")
|
return "", errors.Wrap(err, "stat object")
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.bucketURL.JoinPath(key).String(), nil
|
return config.bucketURL.JoinPath(key).String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *s3) Reload(app *config.App) (err error) {
|
func (s *s3) Reload(app *config.App) (err error) {
|
||||||
@@ -187,29 +199,7 @@ func (s *s3) Reload(app *config.App) (err error) {
|
|||||||
return errors.Wrap(err, "validate config")
|
return errors.Wrap(err, "validate config")
|
||||||
}
|
}
|
||||||
|
|
||||||
if reflect.DeepEqual(s.Config(), newConfig) {
|
|
||||||
log.Debug(ctx, "object storage config not changed")
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
client, err := minio.New(newConfig.Endpoint, &minio.Options{
|
|
||||||
Creds: credentials.NewStaticV4(newConfig.AccessKeyID, newConfig.SecretAccessKey, ""),
|
|
||||||
Secure: true,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "new minio client")
|
|
||||||
}
|
|
||||||
|
|
||||||
u, err := url.Parse(newConfig.BucketURL)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "parse public url")
|
|
||||||
}
|
|
||||||
|
|
||||||
s.client = client
|
|
||||||
s.bucketURL = u
|
|
||||||
s.SetConfig(newConfig)
|
s.SetConfig(newConfig)
|
||||||
|
|
||||||
log.Info(ctx, "object storage reloaded")
|
log.Info(ctx, "object storage reloaded")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user