-
Notifications
You must be signed in to change notification settings - Fork 0
/
minio.go
187 lines (162 loc) · 5.05 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package minio
import (
"context"
minioClient "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/minio/minio-go/v7/pkg/encrypt"
"io"
"net/http"
)
type Client struct {
ctx context.Context
endpoint string
region string
accessKeyID string
secretAccessKey string
useSSL bool
password string
client *minioClient.Client
}
func New(opts ...Option) (*Client, error) {
c := &Client{
ctx: context.Background(),
endpoint: "127.0.0.1:9000",
region: "cn-north-1",
}
for _, opt := range opts {
opt(c)
}
// Initialize minio client object.
minioClient, err := minioClient.New(c.endpoint, &minioClient.Options{
Creds: credentials.NewStaticV4(c.accessKeyID, c.secretAccessKey, ""),
Secure: c.useSSL,
})
if err != nil {
return nil, err
}
c.client = minioClient
return c, nil
}
// MakeBucket Make Bucket
func (c *Client) MakeBucket(bucketName string, objectLocking ...bool) error {
locking := false
if len(objectLocking) > 0 {
locking = objectLocking[0]
}
err := c.client.MakeBucket(c.ctx, bucketName, minioClient.MakeBucketOptions{Region: c.region, ObjectLocking: locking})
if err != nil {
//Check to see if we already own this bucket (which happens if you run this twice)
exists, errBucketExists := c.client.BucketExists(c.ctx, bucketName)
if errBucketExists == nil && exists {
} else {
return err
}
}
return nil
}
// PutObject Put Object
func (c *Client) PutObject(bucketName string, objectName string, reader io.Reader, objectSize int64, userTags map[string]string) (*UploadInfo, error) {
opts := minioClient.PutObjectOptions{}
if userTags != nil {
opts.UserTags = userTags
opts.UserMetadata = userTags
}
if c.password != "" {
opts.ServerSideEncryption = encrypt.DefaultPBKDF([]byte(c.password), []byte(bucketName+objectName))
}
info, err := c.client.PutObject(c.ctx, bucketName, objectName, reader, objectSize, opts)
if err != nil {
return nil, err
}
return &UploadInfo{
Bucket: info.Bucket,
Key: info.Key,
ETag: info.ETag,
Size: info.Size,
LastModified: info.LastModified,
}, nil
}
// FPutObject Put Object
func (c *Client) FPutObject(bucketName string, objectName string, filePath string, userTags map[string]string) (*UploadInfo, error) {
opts := minioClient.PutObjectOptions{}
if userTags != nil {
opts.UserTags = userTags
opts.UserMetadata = userTags
}
if c.password != "" {
opts.ServerSideEncryption = encrypt.DefaultPBKDF([]byte(c.password), []byte(bucketName+objectName))
}
info, err := c.client.FPutObject(c.ctx, bucketName, objectName, filePath, opts)
if err != nil {
return nil, err
}
return &UploadInfo{
Bucket: info.Bucket,
Key: info.Key,
ETag: info.ETag,
Size: info.Size,
LastModified: info.LastModified,
}, nil
}
// GetObjectByte Get Object Byte
func (c *Client) GetObjectByte(bucketName, objectName string) ([]byte, error) {
opts := minioClient.GetObjectOptions{}
if c.password != "" {
opts.ServerSideEncryption = encrypt.DefaultPBKDF([]byte(c.password), []byte(bucketName+objectName))
}
object, err := c.client.GetObject(c.ctx, bucketName, objectName, opts)
if err != nil {
return nil, err
}
buf, err := io.ReadAll(object)
if err != nil {
return nil, err
}
return buf, nil
}
// GetObject Get Object
func (c *Client) GetObject(bucketName, objectName string) (*minioClient.Object, error) {
opts := minioClient.GetObjectOptions{}
if c.password != "" {
opts.ServerSideEncryption = encrypt.DefaultPBKDF([]byte(c.password), []byte(bucketName+objectName))
}
object, err := c.client.GetObject(c.ctx, bucketName, objectName, opts)
if err != nil {
return nil, err
}
return object, nil
}
// GetObjectRead Get Object read
func (c *Client) GetObjectRead(bucketName, objectName string) (io.ReadCloser, *minioClient.ObjectInfo, *http.Header, error) {
opts := minioClient.GetObjectOptions{}
if c.password != "" {
opts.ServerSideEncryption = encrypt.DefaultPBKDF([]byte(c.password), []byte(bucketName+objectName))
}
readCloser, objectInfo, header, err := c.client.GetObjectRead(c.ctx, bucketName, objectName, opts)
return readCloser, &objectInfo, &header, err
}
// StatObject Stat Object
func (c *Client) StatObject(bucketName, objectName string) (*ObjectInfo, error) {
info, err := c.client.StatObject(c.ctx, bucketName, objectName, minioClient.StatObjectOptions{})
if err != nil {
return nil, err
}
return &ObjectInfo{
ETag: info.ETag,
Key: info.Key,
LastModified: info.LastModified,
Size: info.Size,
ContentType: info.ContentType,
UserMetadata: info.UserMetadata,
UserTags: info.UserTags,
UserTagCount: info.UserTagCount,
StorageClass: info.StorageClass,
IsLatest: info.IsLatest,
IsDeleteMarker: info.IsDeleteMarker,
VersionID: info.VersionID,
}, nil
}
// RemoveObject Remove Object
func (c *Client) RemoveObject(bucketName, objectName string) error {
return c.client.RemoveObject(c.ctx, bucketName, objectName, minioClient.RemoveObjectOptions{})
}