change from creation to exists check
All checks were successful
/ Make image (push) Successful in 1m14s

This commit is contained in:
nquidox 2025-10-15 21:34:32 +03:00
parent 38193e8943
commit 95b75d0067
3 changed files with 9 additions and 16 deletions

View file

@ -2,8 +2,6 @@ package mediaStorage
import (
"context"
"errors"
"fmt"
"github.com/minio/minio-go/v7"
log "github.com/sirupsen/logrus"
"io"
@ -21,21 +19,15 @@ func newService(client *minio.Client) *Service {
}
}
func (s *Service) СreateBucketIfNotExists(bucketName string) error {
func (s *Service) CheckBucketExists(bucketName string) (bool, error) {
ctx := context.Background()
err := s.client.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{})
exists, err := s.client.BucketExists(ctx, bucketName)
if err != nil {
var minioErr minio.ErrorResponse
if errors.As(err, &minioErr) {
if minioErr.Code == "BucketAlreadyExists" || minioErr.Code == "BucketAlreadyOwnedByYou" {
log.Infof("Media storage | Bucket %s already exists, skipping creation", bucketName)
return nil
}
}
return fmt.Errorf("failed to create bucket: %w", err)
log.WithError(err).Fatal("Media storage | Failed to check bucket existence")
return exists, err
}
log.Infof("Media storage | Bucket %s created successfully", bucketName)
return nil
log.Infof("Media storage | Bucket %s exists", bucketName)
return exists, nil
}
func (s *Service) Upload(ctx context.Context, bucket, object string, reader io.Reader, size int64) error {