recipes2/internal/media_storage/minio/minio.go

119 lines
3.6 KiB
Go
Raw Normal View History

2024-01-16 21:10:29 +02:00
package minio
import (
2024-01-20 20:33:49 +02:00
"context"
"fmt"
"io"
"log"
2024-01-16 21:10:29 +02:00
"github.com/minio/minio-go/v7"
2024-01-20 20:33:49 +02:00
"github.com/minio/minio-go/v7/pkg/credentials"
)
// Bucket names
2024-01-20 21:01:32 +02:00
const (
recipeImgBucket string = "recipeimg"
2024-01-16 21:10:29 +02:00
)
2024-01-20 20:33:49 +02:00
const location string = "eu_ru"
2024-01-18 18:48:27 +02:00
type ObjStorage struct {
2024-01-16 21:10:29 +02:00
minio *minio.Client
}
2024-01-20 20:33:49 +02:00
func New(ctx context.Context, addr, user, password string) (*ObjStorage, error) {
2024-01-21 12:10:06 +02:00
const op = "media_storage.minio.New"
2024-01-20 20:33:49 +02:00
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
2024-01-20 21:01:32 +02:00
func (o *ObjStorage) uploadFile(ctx context.Context, bucketName string, objectName string, fileBuffer io.Reader, contentType string, fileSize int64) error {
2024-01-21 12:10:06 +02:00
const op = "media_storage.minio.UploadFile"
2024-01-20 20:33:49 +02:00
// 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
2024-01-20 21:01:32 +02:00
func (o *ObjStorage) getFile(ctx context.Context, bucketName string, objectName string) (*minio.Object, error) {
2024-01-21 12:10:06 +02:00
const op = "media_storage.minio.GetFile"
2024-01-20 20:33:49 +02:00
// 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
2024-01-20 21:01:32 +02:00
func (o *ObjStorage) delFile(ctx context.Context, bucketName string, objectName string) error {
2024-01-21 12:10:06 +02:00
const op = "media_storage.minio.DelFile"
2024-01-20 20:33:49 +02:00
err := o.minio.RemoveObject(ctx, bucketName, objectName, minio.RemoveObjectOptions{ForceDelete: true})
if err != nil {
return fmt.Errorf("%s: %w", op, err)
}
return nil
2024-01-16 21:10:29 +02:00
}
2024-01-20 21:01:32 +02:00
2024-01-21 12:10:06 +02:00
// 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
2024-01-20 21:01:32 +02:00
}