package minio import ( "context" "fmt" "io" "log" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" ) // Bucket names const ( recipeImgBucket string = "recipeimg" ) const location string = "eu_ru" type ObjStorage struct { minio *minio.Client } func New(ctx context.Context, addr, user, password string) (*ObjStorage, error) { const op = "media_storage.minio.New" minioClient, err := minio.New(addr, &minio.Options{ Creds: credentials.NewStaticV4(user, password, ""), Secure: false, }) if err != nil { return nil, fmt.Errorf("%s: %w", op, err) } return &ObjStorage{minio: minioClient}, nil } // // MinioConnection func for opening minio connection. // func MinioConnection(bucketName string) (*minio.Client, error) { // ctx := context.Background() // useSSL := false // // Initialize minio client object. // minioClient, errInit := minio.New(config.Conf.MINIO_ADDR, &minio.Options{ // Creds: credentials.NewStaticV4(config.Conf.MINIO_ROOT_USER, config.Conf.MINIO_ROOT_PASSWORD, ""), // Secure: useSSL, // }) // if errInit != nil { // return nil, errInit // } // // Check exists // exists, errBucketExists := minioClient.BucketExists(ctx, bucketName) // if errBucketExists != nil { // return nil, errBucketExists // } // if !exists { // // Create bucket // err := minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location}) // if err != nil { // return nil, err // } else { // log.Printf("Successfully created %s\n", bucketName) // } // } // return minioClient, errInit // } // Upload file to bucket func (o *ObjStorage) uploadFile(ctx context.Context, bucketName string, objectName string, fileBuffer io.Reader, contentType string, fileSize int64) error { const op = "media_storage.minio.UploadFile" // Upload the zip file with PutObject info, err := o.minio.PutObject(ctx, bucketName, objectName, fileBuffer, fileSize, minio.PutObjectOptions{ContentType: contentType}) if err != nil { return fmt.Errorf("%s: %w", op, err) } log.Printf("Successfully uploaded %s of size %d\n", objectName, info.Size) return nil } // Get file from bucket func (o *ObjStorage) getFile(ctx context.Context, bucketName string, objectName string) (*minio.Object, error) { const op = "media_storage.minio.GetFile" // Get object from minio minio_obj, err := o.minio.GetObject(ctx, bucketName, objectName, minio.GetObjectOptions{Checksum: true}) if err != nil { return nil, fmt.Errorf("%s: %w", op, err) } return minio_obj, nil } // Delete file from bucket func (o *ObjStorage) delFile(ctx context.Context, bucketName string, objectName string) error { const op = "media_storage.minio.DelFile" err := o.minio.RemoveObject(ctx, bucketName, objectName, minio.RemoveObjectOptions{ForceDelete: true}) if err != nil { return fmt.Errorf("%s: %w", op, err) } return nil } // SaveRecipeImage saves image to bucket for recipes photos. func (o *ObjStorage) SaveRecipeImage(ctx context.Context, imageFile io.Reader, filename string, contentType string, fileSize int64) error { const op = "media_storage.minio.SaveRecipeImage" err := o.uploadFile(ctx, recipeImgBucket, filename, imageFile, contentType, fileSize) if err != nil { return fmt.Errorf("%s: %w", op, err) } return err } // RecipeImage gets image from recipe's images bucket by filename. func (o *ObjStorage) RecipeImage(ctx context.Context, filename string) (*minio.Object, error) { const op = "media_storage.minio.RecipeImage" obj, err := o.getFile(ctx, recipeImgBucket, filename) if err != nil { return nil, fmt.Errorf("%s: %w", op, err) } return obj, err }