This commit is contained in:
Anang Yusman 2026-01-02 18:07:41 +08:00
commit 60d48cfda3
1 changed files with 30 additions and 14 deletions

View File

@ -2,9 +2,10 @@ package config
import (
"context"
"log"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"log"
)
// MinioSetup struct
@ -30,26 +31,41 @@ func (_minio *MinioStorage) ConnectMinio() (*minio.Client, error) {
bucketName := _minio.Cfg.ObjectStorage.MinioStorage.BucketName
location := _minio.Cfg.ObjectStorage.MinioStorage.Location
// Log connection attempt
log.Printf("[MinIO] Attempting to connect to MinIO at endpoint: %s (SSL: %v)", endpoint, useSSL)
// Initialize minio client object.
minioClient, errInit := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if errInit != nil {
log.Fatalln(errInit)
log.Printf("[MinIO] ERROR: Failed to initialize MinIO client. Endpoint: %s, SSL: %v, Error: %v", endpoint, useSSL, errInit)
return nil, errInit
}
err := minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
if err != nil {
// Check to see if we already own this bucket (which happens if you run this twice)
exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
if errBucketExists == nil && exists {
log.Printf("We already own %s\n", bucketName)
} else {
log.Fatalln(err)
}
} else {
log.Printf("Successfully created %s\n", bucketName)
log.Printf("[MinIO] MinIO client initialized successfully. Checking bucket: %s", bucketName)
// Check if bucket exists first
exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
if errBucketExists != nil {
log.Printf("[MinIO] ERROR: Failed to check if bucket exists. Bucket: %s, Error: %v", bucketName, errBucketExists)
return nil, errBucketExists
}
return minioClient, errInit
if exists {
log.Printf("[MinIO] Bucket '%s' already exists and is accessible", bucketName)
} else {
// Try to create the bucket
log.Printf("[MinIO] Bucket '%s' does not exist. Attempting to create it in location: %s", bucketName, location)
err := minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
if err != nil {
log.Printf("[MinIO] ERROR: Failed to create bucket. Bucket: %s, Location: %s, Error: %v", bucketName, location, err)
return nil, err
}
log.Printf("[MinIO] Successfully created bucket '%s' in location '%s'", bucketName, location)
}
log.Printf("[MinIO] Successfully connected to MinIO and bucket '%s' is ready", bucketName)
return minioClient, nil
}