From 24c72800ad5d426cdeaec59a5624726c5c3cf45d Mon Sep 17 00:00:00 2001 From: LandaMm Date: Sat, 24 May 2025 17:17:31 +0200 Subject: [PATCH] feat: file storage --- internal/storage/mod.go | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 internal/storage/mod.go diff --git a/internal/storage/mod.go b/internal/storage/mod.go new file mode 100644 index 0000000..c104bde --- /dev/null +++ b/internal/storage/mod.go @@ -0,0 +1,57 @@ +package storage + +import ( + "context" + "io" + "log" + "net/url" + "os" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" +) + +type FileStorage struct { + client *minio.Client +} + +func New() *FileStorage { + endpoint := os.Getenv("MINIO_ENDPOINT") + if endpoint == "" { + log.Fatalln("MINIO_ENDPOINT env var is required") + return nil + } + + accessKey := os.Getenv("MINIO_ACCESS_KEY") + if accessKey == "" { + log.Fatalln("MINIO_ACCESS_KEY env var is required") + return nil + } + + secretKey := os.Getenv("MINIO_SECRET_KEY") + if secretKey == "" { + log.Fatalln("MINIO_SECRET_KEY env var is required") + return nil + } + + client, err := minio.New(endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(accessKey, secretKey, ""), + Secure: false, + }) + if err != nil { + log.Fatalln("Failed to create minio client:", err) + return nil + } + + return &FileStorage{ + client, + } +} + +func (fs *FileStorage) PutObject(ctx context.Context, bucketName string, objectName string, reader io.Reader, size int64, opts minio.PutObjectOptions) (minio.UploadInfo, error) { + return fs.client.PutObject(ctx, bucketName, objectName, reader, size, opts) +} + +func (fs *FileStorage) EndpointURL() *url.URL { + return fs.client.EndpointURL() +}