forked from navi-tt/storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcos.go
237 lines (183 loc) · 4.83 KB
/
cos.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
228
229
230
231
232
233
234
235
236
237
package cos
import (
"context"
"encoding/json"
"fmt"
"github.com/navi-tt/storage"
"github.com/tencentyun/cos-go-sdk-v5"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
)
//腾讯云COS存储
type COS struct {
SecretID string
SecretKey string
Host string
Bucket string
Protocol string
}
func (s *tensenCos) Init(cfg string) (storage.Storage, error) {
cosConfig := &COS{}
if err := json.Unmarshal([]byte(cfg), cosConfig); err != nil {
return nil, err
}
fmt.Printf("[COS Init] conf : \n %v \n", cosConfig)
storageUrl := fmt.Sprintf("%s://%s.%s", cosConfig.Protocol, cosConfig.Bucket, cosConfig.Host)
u, _ := url.Parse(storageUrl)
b := &cos.BaseURL{BucketURL: u}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: cosConfig.SecretID,
SecretKey: cosConfig.SecretKey,
},
})
cs.client = c
return cs, nil
}
type tensenCos struct {
client *cos.Client
}
func (s *tensenCos) PutByPath(key string, src string) error {
fmt.Printf("[COS PUT BY PATH] object: %s \n", key)
fd, fi, err := storage.OpenLocal(src)
if err != nil {
return err
}
defer fd.Close()
return s.Put(key, fd, fi.Size())
}
func (s *tensenCos) Put(key string, r io.Reader, contentLength int64) error {
fmt.Printf("[COS PUT] object: %s \n", key)
opt := &cos.ObjectPutOptions{}
_, err := s.client.Object.Put(context.Background(), key, r, opt)
if err != nil {
return err
}
return nil
}
func (s *tensenCos) GetToPath(key string, dest string) error {
fmt.Printf("[COS GET TO PATH] object: %s \n", key)
dir, _ := filepath.Split(dest)
_ = os.MkdirAll(dir, 0766)
fd, err := os.OpenFile(dest, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0766)
if err != nil {
return err
}
defer fd.Close()
return s.Get(key, fd)
}
func (s *tensenCos) Get(key string, wa io.WriterAt) error {
fmt.Printf("[COS GET] object: %s \n", key)
if !storage.ValidKey(key) {
return storage.ErrObjectKeyInvalid
}
opt := &cos.ObjectGetOptions{}
v, err := s.client.Object.Get(context.Background(), key, opt)
if err != nil {
return err
}
return storage.Copy(wa, v.Body)
}
func (s *tensenCos) FileStream(key string) (io.ReadCloser, *storage.FileInfo, error) {
fmt.Printf("[COS FILE STREAM] object: %s \n", key)
if !storage.ValidKey(key) {
return nil, nil, storage.ErrObjectKeyInvalid
}
opt := &cos.ObjectGetOptions{}
output, err := s.client.Object.Get(context.Background(), key, opt)
if err != nil {
if output != nil {
if output.StatusCode == http.StatusNotFound {
return nil, nil, storage.ErrObjectNotFound
}
}
return nil, nil, err
}
modTime, _ := time.Parse(time.RFC1123, output.Header.Get("Last-Modified"))
return output.Body, &storage.FileInfo{
Size: output.ContentLength,
ModTime: modTime.In(time.Local),
Mode: 0666,
}, nil
}
func (s *tensenCos) Stat(key string) (*storage.FileInfo, error) {
fmt.Printf("[COS STAT] object: %s \n", key)
if !storage.ValidKey(key) {
return nil, storage.ErrObjectKeyInvalid
}
opt := &cos.ObjectGetOptions{}
output, err := s.client.Object.Get(context.Background(), key, opt)
if err != nil {
if output != nil {
if output.StatusCode == http.StatusNotFound {
return nil, storage.ErrObjectNotFound
}
}
return nil, err
}
modTime, _ := time.Parse(time.RFC1123, output.Header.Get("Last-Modified"))
return &storage.FileInfo{
Size: output.ContentLength,
ModTime: modTime.In(time.Local),
Mode: 0666,
}, nil
}
func (s *tensenCos) Size(key string) (int64, error) {
fmt.Printf("[COS SIZE] object: %s \n", key)
if !storage.ValidKey(key) {
return 0, storage.ErrObjectKeyInvalid
}
opt := &cos.ObjectGetOptions{}
output, err := s.client.Object.Get(context.Background(), key, opt)
if err != nil {
if output != nil {
if output.StatusCode == http.StatusNotFound {
return 0, storage.ErrObjectNotFound
}
}
return 0, err
}
if output.ContentLength == 0 {
return 0, fmt.Errorf("failed to get object size with code %d", output.StatusCode)
}
return output.ContentLength, nil
}
func (s *tensenCos) IsExist(key string) (bool, error) {
fmt.Printf("[COS IS EXIST] object: %s \n", key)
if !storage.ValidKey(key) {
return false, storage.ErrObjectKeyInvalid
}
opt := &cos.ObjectGetOptions{}
output, err := s.client.Object.Get(context.Background(), key, opt)
if err != nil {
if output != nil {
if output.StatusCode == http.StatusNotFound {
return false, nil
}
}
return false, err
}
return true, nil
}
func (s *tensenCos) Del(key string) error {
fmt.Printf("[COS DEL] object: %s \n", key)
if !storage.ValidKey(key) {
return storage.ErrObjectKeyInvalid
}
resp, err := s.client.Object.Delete(context.Background(), key)
if err != nil {
return err
}
if resp.StatusCode == http.StatusNotFound {
return storage.ErrObjectNotFound
}
return nil
}
var cs = &tensenCos{}
func init() {
storage.Register(storage.COS, cs)
}