forked from beyondstorage/go-service-minio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
193 lines (179 loc) · 4.68 KB
/
storage.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
188
189
190
191
192
193
package minio
import (
"context"
"io"
"net/url"
"strings"
"time"
"github.com/minio/minio-go/v7"
ps "github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/pkg/iowrap"
"github.com/beyondstorage/go-storage/v4/services"
. "github.com/beyondstorage/go-storage/v4/types"
)
const defaultListObjectBufferSize = 100
func (s *Storage) copy(ctx context.Context, src string, dst string, opt pairStorageCopy) (err error) {
srcOpts := minio.CopySrcOptions{
Bucket: s.bucket,
Object: s.getAbsPath(src),
}
dstOpts := minio.CopyDestOptions{
Bucket: s.bucket,
Object: s.getAbsPath(dst),
}
_, err = s.client.CopyObject(ctx, dstOpts, srcOpts)
return err
}
func (s *Storage) create(path string, opt pairStorageCreate) (o *Object) {
rp := s.getAbsPath(path)
if opt.HasObjectMode && opt.ObjectMode.IsDir() {
if !s.features.VirtualDir {
return
}
rp += "/"
o = s.newObject(true)
o.Mode = ModeDir
} else {
o = s.newObject(false)
o.Mode = ModeRead
}
o.ID = rp
o.Path = path
return o
}
func (s *Storage) delete(ctx context.Context, path string, opt pairStorageDelete) (err error) {
rp := s.getAbsPath(path)
if opt.HasObjectMode && opt.ObjectMode.IsDir() {
if !s.features.VirtualDir {
err = services.PairUnsupportedError{Pair: ps.WithObjectMode(opt.ObjectMode)}
return
}
rp += "/"
}
err = s.client.RemoveObject(ctx, s.bucket, rp, minio.RemoveObjectOptions{})
return err
}
func (s *Storage) list(ctx context.Context, path string, opt pairStorageList) (oi *ObjectIterator, err error) {
rp := s.getAbsPath(path)
options := minio.ListObjectsOptions{}
if !opt.HasListMode || opt.ListMode.IsPrefix() {
options.Recursive = true
} else if opt.ListMode.IsDir() {
if !strings.HasSuffix(rp, "/") {
rp += "/"
}
} else {
return nil, services.ListModeInvalidError{Actual: opt.ListMode}
}
options.Prefix = rp
input := &objectPageStatus{
bufferSize: defaultListObjectBufferSize,
options: options,
}
return NewObjectIterator(ctx, s.nextObjectPage, input), nil
}
func (s *Storage) metadata(opt pairStorageMetadata) (meta *StorageMeta) {
meta = NewStorageMeta()
meta.Name = s.bucket
meta.WorkDir = s.workDir
return meta
}
func (s *Storage) nextObjectPage(ctx context.Context, page *ObjectPage) error {
input := page.Status.(*objectPageStatus)
if input.objChan == nil {
input.objChan = s.client.ListObjects(ctx, s.bucket, input.options)
}
for i := 0; i < input.bufferSize; i++ {
v, ok := <-input.objChan
if !ok {
return IterateDone
}
if v.Err != nil {
return v.Err
}
o, err := s.formatFileObject(v)
if err != nil {
return err
}
page.Data = append(page.Data, o)
input.counter++
}
return nil
}
func (s *Storage) reach(ctx context.Context, path string, opt pairStorageReach) (url_ string, err error) {
rp := s.getAbsPath(path)
var expire = time.Hour * 1
if opt.HasExpire {
expire = opt.Expire
}
URL, err := s.client.PresignedGetObject(ctx, s.bucket, rp, expire, url.Values{})
if err != nil {
return "", err
}
return URL.String(), nil
}
func (s *Storage) read(ctx context.Context, path string, w io.Writer, opt pairStorageRead) (n int64, err error) {
rp := s.getAbsPath(path)
output, err := s.client.GetObject(ctx, s.bucket, rp, minio.GetObjectOptions{})
if err != nil {
return 0, err
}
defer func() {
cerr := output.Close()
if cerr != nil {
err = cerr
}
}()
if opt.HasOffset {
_, err = output.Seek(opt.Offset, 0)
if err != nil {
return 0, err
}
}
var rc io.ReadCloser = output
if opt.HasSize {
rc = iowrap.LimitReadCloser(rc, opt.Size)
}
if opt.HasIoCallback {
rc = iowrap.CallbackReadCloser(rc, opt.IoCallback)
}
return io.Copy(w, rc)
}
func (s *Storage) stat(ctx context.Context, path string, opt pairStorageStat) (o *Object, err error) {
rp := s.getAbsPath(path)
if opt.HasObjectMode && opt.ObjectMode.IsDir() {
if !s.features.VirtualDir {
err = services.PairUnsupportedError{Pair: ps.WithObjectMode(opt.ObjectMode)}
return
}
rp += "/"
}
output, err := s.client.StatObject(ctx, s.bucket, rp, minio.StatObjectOptions{})
if err != nil {
return nil, err
}
o, err = s.formatFileObject(output)
if err != nil {
return nil, err
}
return
}
func (s *Storage) write(ctx context.Context, path string, r io.Reader, size int64, opt pairStorageWrite) (n int64, err error) {
rp := s.getAbsPath(path)
r = io.LimitReader(r, size)
if opt.HasIoCallback {
r = iowrap.CallbackReader(r, opt.IoCallback)
}
options := minio.PutObjectOptions{}
if opt.HasContentType {
options.ContentType = opt.ContentType
}
if opt.HasStorageClass {
options.StorageClass = opt.StorageClass
}
_, err = s.client.PutObject(ctx, s.bucket, rp, r, size, options)
if err != nil {
return 0, err
}
return size, nil
}