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
|
|
|
|
var (
|
|
|
|
RecipeImg 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) {
|
|
|
|
const op = "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 = "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 = "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 = "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
|
2024-01-16 21:10:29 +02:00
|
|
|
}
|