-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcos.go
278 lines (252 loc) · 6.76 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package cos
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/tencentyun/cos-go-sdk-v5"
)
type (
Cos struct {
Bucket string
AccessKey string
SecretKey string
Region string
Source string
Target string
StripPrefix string
Endpoint string
Include string
Exclude string
}
Ext struct {
AutoTime bool
TimeFormat string
Debug bool
Pause bool
Proxy string
}
Plugin struct {
Cos Cos
Ext Ext
}
)
// Exec executes the plugin step
func (p Plugin) Exec() error {
logrus.Info("start upload...")
p.upload()
logrus.Info("upload success")
return nil
}
func (p Plugin) upload() {
op := &UploadOptions{
// StorageClass: storageClass,
// RateLimiting: rateLimiting,
// PartSize: partSize,
ThreadNum: 10,
}
if p.Cos.Endpoint == "" {
p.Cos.Endpoint = fmt.Sprintf("cos.%s.myqcloud.com", p.Cos.Region)
}
c := CreateClient(p.Cos.AccessKey, p.Cos.SecretKey, p.Cos.Endpoint, p.Cos.Bucket)
if p.Ext.AutoTime {
p.Cos.Target = fmt.Sprintf("%s/%s", strings.TrimSuffix(p.Cos.Target, "/"), time.Now().Format(p.Ext.TimeFormat))
}
MultiUpload(c, p.Cos.Source, p.Cos.Bucket, p.Cos.Target, p.Cos.Include, p.Cos.Exclude, op)
}
// 根据函数参数创建客户端
func CreateClient(secretID, secretKey, endpoint, bucketIDName string) *cos.Client {
return cos.NewClient(CreateURL(bucketIDName, "https", endpoint), &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: secretID,
SecretKey: secretKey,
},
})
}
func GenBucketURL(bucketIDName string, protocol string, endpoint string) string {
b := fmt.Sprintf("%s://%s.%s", protocol, bucketIDName, endpoint)
return b
}
func GenServiceURL(protocol string, endpoint string) string {
s := fmt.Sprintf("%s://%s", protocol, endpoint)
return s
}
func GenCiURL(bucketIDName string, protocol string, endpoint string) string {
c := fmt.Sprintf("%s://%s.%s", protocol, bucketIDName, endpoint)
return c
}
// 根据函数参数生成URL
func CreateURL(idName string, protocol string, endpoint string) *cos.BaseURL {
b := GenBucketURL(idName, protocol, endpoint)
s := GenServiceURL(protocol, endpoint)
c := GenCiURL(idName, protocol, endpoint)
bucketURL, _ := url.Parse(b)
serviceURL, _ := url.Parse(s)
ciURL, _ := url.Parse(c)
return &cos.BaseURL{
BucketURL: bucketURL,
ServiceURL: serviceURL,
CIURL: ciURL,
}
}
type UploadOptions struct {
StorageClass string
RateLimiting float32
PartSize int64
ThreadNum int
}
func UploadPathFixed(localPath string, targetPath string) (string, string) {
// eg:~/example/123.txt => cos://bucket/path/123.txt
// 0. ~/example/123.txt => cos://bucket
if targetPath == "" {
pathList := strings.Split(localPath, "/")
fileName := pathList[len(pathList)-1]
targetPath = fileName
}
// 1. ~/example/123.txt => cos://bucket/path/
s, err := os.Stat(localPath)
if err != nil {
logrus.Fatal(err)
os.Exit(1)
}
if s.IsDir() {
fileNames := strings.Split(localPath, "/")
fileName := fileNames[len(fileNames)-1]
targetPath = targetPath + fileName
}
// 2. 123.txt => cos://bucket/path/
if !filepath.IsAbs(localPath) {
dirPath, err := os.Getwd()
if err != nil {
logrus.Fatalln(err)
os.Exit(1)
}
localPath = dirPath + "/" + localPath
}
return localPath, targetPath
}
func SingleUpload(c *cos.Client, sourcePath, bucketName, targetPath string, op *UploadOptions) {
opt := &cos.MultiUploadOptions{
OptIni: &cos.InitiateMultipartUploadOptions{
ACLHeaderOptions: &cos.ACLHeaderOptions{
XCosACL: "",
XCosGrantRead: "",
XCosGrantWrite: "",
XCosGrantFullControl: "",
XCosGrantReadACP: "",
XCosGrantWriteACP: "",
},
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
CacheControl: "",
ContentDisposition: "",
ContentEncoding: "",
ContentType: "",
ContentMD5: "",
ContentLength: 0,
ContentLanguage: "",
Expect: "",
Expires: "",
XCosContentSHA1: "",
XCosMetaXXX: nil,
XCosStorageClass: op.StorageClass,
XCosServerSideEncryption: "",
XCosSSECustomerAglo: "",
XCosSSECustomerKey: "",
XCosSSECustomerKeyMD5: "",
XOptionHeader: nil,
XCosTrafficLimit: (int)(op.RateLimiting * 1024 * 1024 * 8),
// Listener: &CosListener{},
},
},
PartSize: op.PartSize,
ThreadPoolSize: op.ThreadNum,
CheckPoint: true,
// EnableVerification: false,
}
sourcePath, targetPath = UploadPathFixed(sourcePath, targetPath)
logrus.Infof("Upload %s => cos://%s/%s\n", sourcePath, bucketName, targetPath)
_, _, err := c.Object.Upload(context.Background(), targetPath, sourcePath, opt)
if err != nil {
logrus.Errorf("upload: %s,err: %v", sourcePath, err)
}
}
func MultiUpload(c *cos.Client, sourceDir, bucketName, targetDir, include, exclude string, op *UploadOptions) {
var files []string
if isDir(sourceDir) {
if sourceDir != "" && (sourceDir[len(sourceDir)-1] != '/' && sourceDir[len(sourceDir)-1] != '\\') {
sourceDir += "/"
}
files = GetLocalFilesListRecursive(sourceDir, include, exclude)
} else {
s := strings.Split(sourceDir, "/")
sourceDir = strings.TrimSuffix(sourceDir, s[len(s)-1])
files = append(files, s[len(s)-1])
}
if targetDir != "" && targetDir[len(targetDir)-1] != '/' {
targetDir += "/"
}
for _, f := range files {
sourcePath := sourceDir + f
targetPath := targetDir + f
SingleUpload(c, sourcePath, bucketName, targetPath, op)
}
}
func GetLocalFilesListRecursive(sourcePath string, include string, exclude string) (files []string) {
// bfs遍历文件夹
var dirs []string
dirs = append(dirs, sourcePath)
for len(dirs) > 0 {
dirName := dirs[0]
dirs = dirs[1:]
fileInfos, err := ioutil.ReadDir(dirName)
if err != nil {
logrus.Fatalln(err)
os.Exit(1)
}
for _, f := range fileInfos {
fileName := dirName + "/" + f.Name()
if f.IsDir() {
dirs = append(dirs, fileName)
} else {
fileName = fileName[len(sourcePath)+1:]
files = append(files, fileName)
}
}
}
if len(include) > 0 {
files = MatchPattern(files, include, true)
}
if len(exclude) > 0 {
files = MatchPattern(files, exclude, false)
}
return files
}
func MatchPattern(strs []string, pattern string, include bool) []string {
res := make([]string, 0)
re := regexp.MustCompile(pattern)
for _, s := range strs {
match := re.MatchString(s)
if !include {
match = !match
}
if match {
res = append(res, s)
}
}
return res
}
// isDir 是否为目录
func isDir(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.IsDir()
}