lifecam/lifecam/minio.go

82 lines
1.4 KiB
Go

package lifecam
import (
"context"
"fmt"
"log"
"path/filepath"
"strings"
"git.sr.ht/~kisom/goutils/config"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
const MimeZip = "application/zip"
type Server struct {
client *minio.Client
bucket string
}
func NewMinio() (*Server, error) {
var fields []string
server := config.Get("MINIO_SERVER")
if server == "" {
fields = append(fields, "MINIO_SERVER")
}
access := config.Get("MINIO_ACCESS")
if access == "" {
fields = append(fields, "MINIO_ACCESS")
}
secret := config.Get("MINIO_SECRET")
if secret == "" {
fields = append(fields, "MINIO_SECRET")
}
bucket := config.Get("MINIO_BUCKET")
if bucket == "" {
fields = append(fields, "MINIO_BUCKET")
}
srv := &Server{
bucket: bucket,
}
if len(fields) != 0 {
err := fmt.Errorf("lifecam: missing fields in S3 config: %s",
strings.Join(fields, ", "))
return nil, err
}
var err error
srv.client, err = minio.New(server, &minio.Options{
Creds: credentials.NewStaticV4(access, secret, ""),
Secure: true,
})
if err != nil {
return nil, err
}
return srv, nil
}
func (srv *Server) Upload(path, contentType string) (int64, error) {
log.Printf("uploading %s to minio", path)
ui, err := srv.client.FPutObject(
context.Background(),
srv.bucket,
filepath.Base(path),
path,
minio.PutObjectOptions{
ContentType: contentType,
})
if err != nil {
return 0, err
}
return ui.Size, nil
}