50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package minio
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
)
|
|
|
|
// Must match deployment/Dockerfile.minio MINIO_ROOT_USER / MINIO_ROOT_PASSWORD.
|
|
const (
|
|
BucketName = "dev-minio"
|
|
endpoint = "main-minio:9000"
|
|
accessKeyID = "MINIO_ROOT_USER"
|
|
secretAccessKey = "MINIO_ROOT_PASSWORD"
|
|
)
|
|
|
|
func ConnectionMinio() (*minio.Client, error) {
|
|
minioClient, errInit := minio.New(endpoint, &minio.Options{
|
|
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
|
Secure: false,
|
|
})
|
|
if errInit != nil {
|
|
fmt.Println("minio connection:", errInit)
|
|
return nil, errInit
|
|
}
|
|
return minioClient, nil
|
|
}
|
|
|
|
// EnsureBucket creates the app bucket when it does not exist yet.
|
|
func EnsureBucket(ctx context.Context, client *minio.Client) error {
|
|
exists, err := client.BucketExists(ctx, BucketName)
|
|
if err != nil {
|
|
return fmt.Errorf("bucket exists check: %w", err)
|
|
}
|
|
if exists {
|
|
return nil
|
|
}
|
|
if err := client.MakeBucket(ctx, BucketName, minio.MakeBucketOptions{}); err != nil {
|
|
// Another request may have created it concurrently.
|
|
exists, checkErr := client.BucketExists(ctx, BucketName)
|
|
if checkErr == nil && exists {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("make bucket: %w", err)
|
|
}
|
|
return nil
|
|
}
|