-
Notifications
You must be signed in to change notification settings - Fork 0
/
minio.go
67 lines (63 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
package maleominio
import (
"context"
"github.com/minio/minio-go/v7"
"github.com/tigorlazuardi/maleo/bucket"
"time"
)
type Minio struct {
*minio.Client
bucket string
opts *Option
}
func (m Minio) Upload(ctx context.Context, files []bucket.File) []bucket.UploadResult {
results := make([]bucket.UploadResult, 0, len(files))
for _, f := range files {
func(f bucket.File) {
defer func() { _ = f.Close() }()
filename := m.opts.prefix.String() + f.Filename()
objectURL := m.Client.EndpointURL().String() + "/" + m.bucket + "/" + filename
_, err := m.PutObject(ctx, m.bucket, filename, f, int64(f.Size()), m.opts.putObjectOptions(ctx, f))
results = append(results, bucket.UploadResult{
URL: objectURL,
File: f,
Error: err,
})
}(f)
}
return results
}
// Wrap wraps minio client to tower bucket implementation.
//
// Client credentials must have permission to write access to target bucket.
//
// If client has permission to list and create bucket, it will be used to check if bucket is not exist on initialization,
// and create the bucket if it is not exist. If client does not have such permissions, it will be silently ignored.
//
// You may operate how bucket creation and put object operation is executed by providing options.
func Wrap(client *minio.Client, bucketName string, options ...WrapOption) bucket.Bucket {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
opts := &Option{
prefix: StringerFunc(func() string {
return ""
}),
putObjectOptions: func(ctx context.Context, file bucket.File) minio.PutObjectOptions {
return minio.PutObjectOptions{
ContentType: file.ContentType(),
}
},
}
for _, o := range options {
o.apply(opts)
}
if buckets, err := client.ListBuckets(ctx); err == nil {
for _, b := range buckets {
if b.Name == bucketName {
return Minio{client, bucketName, opts}
}
}
_ = client.MakeBucket(ctx, bucketName, opts.makeBucketOption)
}
return &Minio{client, bucketName, opts}
}