~sircmpwn/core-go

e81358e299f8e345997532ee58b06227f982c2e4 — Simon Ser 5 months ago 1063e5b
s3: add NewClient and URL

Centralizes the logic to create an S3 client and get the base URL.
1 files changed, 27 insertions(+), 0 deletions(-)

M s3/middleware.go
M s3/middleware.go => s3/middleware.go +27 -0
@@ 2,9 2,12 @@ package s3

import (
	"context"
	"errors"
	"net/http"

	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
	"github.com/vaughan0/go-ini"
)

var minioCtxKey = &contextKey{"minio"}


@@ 34,3 37,27 @@ func ForContext(ctx context.Context) *minio.Client {
	}
	return raw
}

var ErrDisabled = errors.New("object storage is not enabled for this server")

func NewClient(conf ini.File) (*minio.Client, error) {
	upstream, _ := conf.Get("objects", "s3-upstream")
	accessKey, _ := conf.Get("objects", "s3-access-key")
	secretKey, _ := conf.Get("objects", "s3-secret-key")
	if upstream == "" || accessKey == "" || secretKey == "" {
		return nil, ErrDisabled
	}

	return minio.New(upstream, &minio.Options{
		Creds:  credentials.NewStaticV4(accessKey, secretKey, ""),
		Secure: true,
	})
}

func URL(conf ini.File, bucket string) string {
	upstream, _ := conf.Get("objects", "s3-upstream")
	if upstream == "" {
		return ""
	}
	return "https://" + upstream
}