Eric Bower
·
18 Feb 24
minio.go
1package storage
2
3import (
4 "context"
5 "fmt"
6 "io"
7 "os"
8 "path/filepath"
9
10 "github.com/minio/minio-go/v7"
11 sst "github.com/picosh/pobj/storage"
12)
13
14type StorageMinio struct {
15 *sst.StorageMinio
16}
17
18func NewStorageMinio(address, user, pass string) (*StorageMinio, error) {
19 st, err := sst.NewStorageMinio(address, user, pass)
20 if err != nil {
21 return nil, err
22 }
23 return &StorageMinio{st}, nil
24}
25
26func (s *StorageMinio) ServeObject(bucket sst.Bucket, fpath string, opts *ImgProcessOpts) (io.ReadCloser, string, error) {
27 if opts == nil || os.Getenv("IMGPROXY_URL") == "" {
28 contentType := GetMimeType(fpath)
29 rc, _, _, err := s.GetObject(bucket, fpath)
30 return rc, contentType, err
31 }
32
33 filePath := filepath.Join(bucket.Name, fpath)
34 dataURL := fmt.Sprintf("s3://%s", filePath)
35 return HandleProxy(dataURL, opts)
36}
37
38func (s *StorageMinio) GetObjectSize(bucket sst.Bucket, fpath string) (int64, error) {
39 info, err := s.Client.StatObject(context.Background(), bucket.Name, fpath, minio.StatObjectOptions{})
40 if err != nil {
41 return 0, err
42 }
43 return info.Size, nil
44}