Files
mrvacommander/pkg/common/minio.go
Michael Hohn 5bdbd60cc5 update s3 endpoint handling
virtual host extracts bucket name from endpoint environment variable;
path uses fixed bucket name
2025-05-13 10:47:54 -07:00

40 lines
1.0 KiB
Go

package common
import (
"context"
"log/slog"
"github.com/minio/minio-go/v7"
)
func CreateMinIOBucketIfNotExists(client *minio.Client, bucketName string) error {
ctx := context.Background()
exists, err := client.BucketExists(ctx, bucketName)
if err != nil {
return err
}
if !exists {
// if env.Get("MRVA_S3_PATHSTYLE") == "true" {}
slog.Info("Creating bucket", "name", bucketName)
err = client.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{})
if err != nil {
// The bucket might already exist at this stage if another component created it concurrently.
// For example, the server might have attempted to create it at the same time as the agent.
if err.(minio.ErrorResponse).Code == "BucketAlreadyOwnedByYou" {
slog.Info("Failed to create bucket because it already exists", "name", bucketName)
return nil
} else {
return err
}
} else {
slog.Info("Bucket created successfully", "name", bucketName)
}
} else {
slog.Info("Bucket already exists", "name", bucketName)
}
return nil
}