nlink/storage.go

74 lines
1.2 KiB
Go
Raw Normal View History

2023-05-06 07:14:37 +00:00
package main
import (
"context"
"io"
"os"
"git.wntrmute.dev/kyle/goutils/config"
"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,
})
return err
}
func restoreDatabase(path string) error {
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 {
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
}