-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
minio.go
82 lines (66 loc) · 1.99 KB
/
minio.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package storage
import (
"io"
"github.com/minio/minio-go"
"github.com/zekroTJA/shinpuru/internal/services/config"
)
// Minio implements the Storage interface for
// the MinIO SDK to connect to a MinIO instance,
// Amazon S3 or Google Cloud.
type Minio struct {
client *minio.Client
location string
}
var _ Storage = (*Minio)(nil)
func (m *Minio) Connect(cfg config.Provider) (err error) {
c := cfg.Config().Storage.Minio
m.client, err = minio.New(c.Endpoint, c.AccessKey, c.AccessSecret, c.Secure)
m.location = c.Location
return
}
func (m *Minio) Status() error {
_, err := m.client.ListBuckets()
return err
}
func (m *Minio) BucketExists(name string) (bool, error) {
return m.client.BucketExists(name)
}
func (m *Minio) CreateBucket(name string, location ...string) error {
return m.client.MakeBucket(name, m.getLocation(location))
}
func (m *Minio) CreateBucketIfNotExists(name string, location ...string) (err error) {
ok, err := m.BucketExists(name)
if err == nil && !ok {
err = m.CreateBucket(name, location...)
}
return
}
func (m *Minio) PutObject(bucketName, objectName string, reader io.Reader, objectSize int64, mimeType string) (err error) {
if err = m.CreateBucketIfNotExists(bucketName, m.location); err != nil {
return
}
_, err = m.client.PutObject(bucketName, objectName, reader, objectSize, minio.PutObjectOptions{
ContentType: mimeType,
})
return
}
func (m *Minio) GetObject(bucketName, objectName string) (io.ReadCloser, int64, error) {
obj, err := m.client.GetObject(bucketName, objectName, minio.GetObjectOptions{})
if err != nil {
return nil, 0, err
}
stat, err := obj.Stat()
if err != nil {
return nil, 0, err
}
return obj, stat.Size, err
}
func (m *Minio) DeleteObject(bucketName, objectName string) error {
return m.client.RemoveObject(bucketName, objectName)
}
func (m *Minio) getLocation(loc []string) string {
if len(loc) > 0 {
return loc[0]
}
return m.location
}