nlink/storage.go

96 lines
1.6 KiB
Go

package main
import (
"context"
"errors"
"io"
"os"
"git.wntrmute.dev/kyle/goutils/config"
"git.wntrmute.dev/kyle/goutils/log"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
var minioClient *minio.Client
func connectMinio() (err error) {
minioClient, err = minio.New(config.Get("minio_host"), &minio.Options{
Creds: credentials.NewStaticV4(
config.Get("minio_access_key"),
config.Get("minio_secret_key"), ""),
Secure: true,
})
if err != nil {
log.Errf("while connecting minio: %s", err)
}
return err
}
func restoreDatabase(path string) error {
if minioClient == nil {
if err := connectMinio(); err != nil {
return err
}
}
if minioClient == nil {
return errors.New("storage: minio client is not connected")
}
obj, err := minioClient.GetObject(
context.Background(),
config.Get("minio_bucket"),
"nlink.db",
minio.GetObjectOptions{},
)
if err != nil {
return err
}
dbFile, err := os.Create(path)
if err != nil {
return err
}
defer dbFile.Close()
_, err = io.Copy(dbFile, obj)
return err
}
func saveDatabase(path string) error {
if minioClient == nil {
if err := connectMinio(); err != nil {
return err
}
}
dbFile, err := os.Open(path)
if err != nil {
return err
}
defer dbFile.Close()
fi, err := dbFile.Stat()
if err != nil {
return err
}
_, err = minioClient.PutObject(
context.Background(),
config.Get("minio_bucket"),
"nlink.db",
dbFile,
fi.Size(),
minio.PutObjectOptions{ContentType: "application/octet-stream"},
)
if err != nil {
return err
}
return nil
}