fsys/storage.go

56 lines
1,014 B
Go
Raw Normal View History

2024-11-17 16:00:48 +01:00
package fsys
import (
"errors"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
var (
ErrInvalidStorageType = errors.New("invalid storage type")
ErrInvalidPath = errors.New("invalid path")
ErrFileNotFound = errors.New("file not found")
)
type Storage struct {
config *Config
s3Client *minio.Client
}
type Config struct {
Type string
Path string
S3Endpoint string
S3Location string
S3Secure bool
S3AccessID string
S3AccessKey string
S3BucketName string
}
func New(config *Config) (*Storage, error) {
newStorage := new(Storage)
newStorage.config = config
switch config.Type {
case "minio", "s3":
s3Client, err := minio.New(config.S3Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(config.S3AccessID, config.S3AccessKey, ""),
Secure: config.S3Secure,
})
if err != nil {
return nil, err
}
newStorage.s3Client = s3Client
case "local":
default:
return nil, ErrInvalidStorageType
}
return newStorage, nil
}