-
Notifications
You must be signed in to change notification settings - Fork 402
/
minio.go
133 lines (114 loc) · 3.12 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package s3client
import (
"bytes"
"io"
"io/ioutil"
minio "github.com/minio/minio-go"
"github.com/zeebo/errs"
)
// MinioError is class for minio errors
var MinioError = errs.Class("minio error")
// Minio implements basic S3 Client with minio
type Minio struct {
api *minio.Client
}
// NewMinio creates new Client
func NewMinio(conf Config) (Client, error) {
api, err := minio.New(conf.S3Gateway, conf.AccessKey, conf.SecretKey, !conf.NoSSL)
if err != nil {
return nil, MinioError.Wrap(err)
}
return &Minio{api}, nil
}
// MakeBucket makes a new bucket
func (client *Minio) MakeBucket(bucket, location string) error {
err := client.api.MakeBucket(bucket, location)
if err != nil {
return MinioError.Wrap(err)
}
return nil
}
// RemoveBucket removes a bucket
func (client *Minio) RemoveBucket(bucket string) error {
err := client.api.RemoveBucket(bucket)
if err != nil {
return MinioError.Wrap(err)
}
return nil
}
// ListBuckets lists all buckets
func (client *Minio) ListBuckets() ([]string, error) {
buckets, err := client.api.ListBuckets()
if err != nil {
return nil, MinioError.Wrap(err)
}
names := []string{}
for _, bucket := range buckets {
names = append(names, bucket.Name)
}
return names, nil
}
// Upload uploads object data to the specified path
func (client *Minio) Upload(bucket, objectName string, data []byte) error {
_, err := client.api.PutObject(
bucket, objectName,
bytes.NewReader(data), int64(len(data)),
minio.PutObjectOptions{ContentType: "application/octet-stream"})
if err != nil {
return MinioError.Wrap(err)
}
return nil
}
// UploadMultipart uses multipart uploads, has hardcoded threshold
func (client *Minio) UploadMultipart(bucket, objectName string, data []byte, threshold int) error {
_, err := client.api.PutObject(
bucket, objectName,
bytes.NewReader(data), -1,
minio.PutObjectOptions{ContentType: "application/octet-stream"})
if err != nil {
return MinioError.Wrap(err)
}
return nil
}
// Download downloads object data
func (client *Minio) Download(bucket, objectName string, buffer []byte) ([]byte, error) {
reader, err := client.api.GetObject(bucket, objectName, minio.GetObjectOptions{})
if err != nil {
return nil, MinioError.Wrap(err)
}
defer func() { _ = reader.Close() }()
n, err := reader.Read(buffer[:cap(buffer)])
if err != io.EOF {
rest, err := ioutil.ReadAll(reader)
if err == io.EOF {
err = nil
}
if err != nil {
return nil, MinioError.Wrap(err)
}
buffer = append(buffer, rest...)
n = len(buffer)
}
buffer = buffer[:n]
return buffer, nil
}
// Delete deletes object
func (client *Minio) Delete(bucket, objectName string) error {
err := client.api.RemoveObject(bucket, objectName)
if err != nil {
return MinioError.Wrap(err)
}
return nil
}
// ListObjects lists objects
func (client *Minio) ListObjects(bucket, prefix string) ([]string, error) {
doneCh := make(chan struct{})
defer close(doneCh)
names := []string{}
for message := range client.api.ListObjects(bucket, prefix, false, doneCh) {
names = append(names, message.Key)
}
return names, nil
}