/
qiniu.go
227 lines (206 loc) · 5.71 KB
/
qiniu.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package store
import (
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/service-sdk/go-sdk-qn/v2/operation"
)
var (
QiniuNotConfigError = fmt.Errorf("qiniu is not configured")
)
// variables compatible with Qiniu rust sdk
func init() {
if v, e := os.LookupEnv(QiniuEnv); e {
if _, qe := os.LookupEnv(operation.QINIU_ENV); !qe {
_ = os.Setenv(operation.QINIU_ENV, v)
}
}
}
func NewQiniuStore(cfgPath string) (Interface, error) {
cfg, err := operation.Load(cfgPath)
if err != nil {
return nil, err
}
return &QiniuStore{
downloader: operation.NewDownloader(cfg),
uploader: operation.NewUploader(cfg),
lister: operation.NewLister(cfg),
}, nil
}
// NewMultiClusterQiniuStore initializes a multi-cluster qiniu store,
// only support QINIU_MULTI_CLUSTER environment variable
func NewMultiClusterQiniuStore() (Interface, error) {
if _, e := os.LookupEnv(operation.QINIU_MULTI_CLUSTER_ENV); !e {
return nil, QiniuNotConfigError
}
return &QiniuStore{
downloader: operation.NewDownloaderV2(),
uploader: operation.NewUploaderV2(),
lister: operation.NewListerV2(),
}, nil
}
type QiniuStore struct {
downloader *operation.Downloader
uploader *operation.Uploader
lister *operation.Lister
}
func (s *QiniuStore) ListPrefix(key string) ([]string, error) {
if s == nil {
return nil, QiniuNotConfigError
}
key = strings.TrimPrefix(key, "/")
start := time.Now()
defer func() {
log.Debugw("list prefix", "key", key, "took", time.Since(start))
}()
return s.lister.ListPrefix(key), nil
}
// UploadData upload memory data to Qiniu store.
func (s *QiniuStore) UploadData(data []byte, key string) (err error) {
if s == nil {
return QiniuNotConfigError
}
key = strings.TrimPrefix(key, "/")
start := time.Now()
defer func() {
log.Debugw("upload data", "key", key, "took", time.Since(start))
}()
return s.uploader.UploadData(data, key)
}
// Upload uploads a file to qiniu.
func (s *QiniuStore) Upload(file string, key string) (err error) {
if s == nil {
return QiniuNotConfigError
}
key = strings.TrimPrefix(key, "/")
start := time.Now()
defer func() {
log.Debugw("upload", "file", file, "key", key, "took", time.Since(start))
}()
return s.uploader.Upload(file, key)
}
// UploadReader upload reader to the Qiniu store.
func (s *QiniuStore) UploadReader(reader io.Reader, _ int64, key string) (err error) {
if s == nil {
return QiniuNotConfigError
}
key = strings.TrimPrefix(key, "/")
start := time.Now()
defer func() {
log.Debugw("upload reader", "key", key, "took", time.Since(start))
}()
return s.uploader.UploadReader(reader, key)
}
// DeleteDirectory deletes a directory from the Qiniu store.
func (s *QiniuStore) DeleteDirectory(dir string) (err error) {
if s == nil {
return QiniuNotConfigError
}
dir = strings.TrimPrefix(dir, "/")
start := time.Now()
defer func() {
log.Debugw("delete directory", "dir", dir, "took", time.Since(start))
}()
_, err = s.lister.DeleteDirectory(dir)
return
}
// Delete deletes a file from the Qiniu store.
func (s *QiniuStore) Delete(key string) (err error) {
if s == nil {
return QiniuNotConfigError
}
key = strings.TrimPrefix(key, "/")
start := time.Now()
defer func() {
log.Debugw("delete", "key", key, "took", time.Since(start))
}()
return s.lister.Delete(key)
}
// Exists returns true if the key exists in the Qiniu store.
func (s *QiniuStore) Exists(key string) (bool, error) {
if s == nil {
return false, QiniuNotConfigError
}
key = strings.TrimPrefix(key, "/")
start := time.Now()
defer func() {
log.Debugw("check exists", "key", key, "took", time.Since(start))
}()
_, err := s.downloader.DownloadCheck(key)
if err != nil {
return false, nil
}
return true, nil
}
// Stat returns the FileInfo for the named file.
func (s *QiniuStore) Stat(key string) (FileStat, error) {
if s == nil {
return FileStat{}, QiniuNotConfigError
}
key = strings.TrimPrefix(key, "/")
start := time.Now()
defer func() {
log.Debugw("get stat", "key", key, "took", time.Since(start))
}()
n, err := s.downloader.DownloadCheck(key)
if err != nil {
return FileStat{}, err
}
return FileStat{
Size: n,
}, nil
}
func (s *QiniuStore) DownloadBytes(key string) ([]byte, error) {
if s == nil {
return nil, QiniuNotConfigError
}
key = strings.TrimPrefix(key, "/")
start := time.Now()
defer func() {
log.Debugw("download bytes", "key", key, "took", time.Since(start))
}()
return s.downloader.DownloadBytes(key)
}
func (s *QiniuStore) DownloadReader(key string) (io.ReadCloser, error) {
if s == nil {
return nil, QiniuNotConfigError
}
key = strings.TrimPrefix(key, "/")
start := time.Now()
defer func() {
log.Debugw("download reader", "key", key, "took", time.Since(start))
}()
resp, err := s.downloader.DownloadRaw(key, http.Header{})
if err != nil || resp.StatusCode != 200 {
return nil, fmt.Errorf("%d: %s", resp.StatusCode, err)
}
return resp.Body, nil
}
func (s *QiniuStore) DownloadRangeBytes(key string, offset int64, size int64) ([]byte, error) {
if s == nil {
return nil, QiniuNotConfigError
}
key = strings.TrimPrefix(key, "/")
start := time.Now()
defer func() {
log.Debugw("download range bytes", "key", key, "offset", offset, "size", size, "took", time.Since(start))
}()
_, data, err := s.downloader.DownloadRangeBytes(key, offset, size)
return data, err
}
func (s *QiniuStore) DownloadRangeReader(key string, offset int64, size int64) (io.ReadCloser, error) {
if s == nil {
return nil, QiniuNotConfigError
}
key = strings.TrimPrefix(key, "/")
start := time.Now()
defer func() {
log.Debugw("download range reader", "key", key, "offset", offset, "size", size, "took", time.Since(start))
}()
_, reader, err := s.downloader.DownloadRangeReader(key, offset, size)
return reader, err
}
var _ Interface = &QiniuStore{}