Files

61 lines
1.4 KiB
Go

package storage
import (
"context"
"fmt"
"net/url"
"strings"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
type MinIOConfig struct {
Endpoint string
AccessKey string
SecretKey string
UseSSL bool
RecordsBucket string
LivestreamBucket string
PresignTTL time.Duration
}
func cleanEndpoint(ep string) string {
ep = strings.TrimSpace(ep)
if ep == "" {
return ep
}
// If a scheme is present, strip it.
if u, err := url.Parse(ep); err == nil && u.Host != "" {
ep = u.Host // drops scheme and any path/query
}
// Remove any trailing slash that might remain.
ep = strings.TrimSuffix(ep, "/")
return ep
}
func NewMinio(c MinIOConfig) (*minio.Client, error) {
endpoint := cleanEndpoint(c.Endpoint)
return minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(c.AccessKey, c.SecretKey, ""),
Secure: c.UseSSL,
})
}
func EnsureBuckets(mc *minio.Client, c MinIOConfig) error {
ctx := context.Background()
for _, b := range []string{c.RecordsBucket, c.LivestreamBucket} {
exists, err := mc.BucketExists(ctx, b)
if err != nil {
return err
}
if !exists {
if err := mc.MakeBucket(ctx, b, minio.MakeBucketOptions{}); err != nil {
return fmt.Errorf("make bucket %s: %w", b, err)
}
}
}
return nil
}