commit 61e08d00608a326e0c7368328e40d7ddfd18456a Author: rainerosion Date: Sun Apr 23 17:56:29 2023 +0800 init: Complete plug-in coding diff --git a/cos.go b/cos.go new file mode 100644 index 0000000..a75cf40 --- /dev/null +++ b/cos.go @@ -0,0 +1,77 @@ +package main + +import ( + "github.com/upyun/go-sdk/v3/upyun" + "io/fs" + "log" + "os" + "path/filepath" + "strings" +) + +func main() { + var ( + operator = GetEnv("up_operator") + bucket = GetEnv("up_bucket") + password = GetEnv("up_password") + localBasePath = GetEnv("local_base_path") + remoteBasePath = GetEnv("remote_base_path") + ) + yun := upyun.NewUpYun(&upyun.UpYunConfig{ + Bucket: bucket, + Operator: operator, + Password: password, + }) + var fileList []string + var err error = nil + filepath.WalkDir(localBasePath, func(path string, d fs.DirEntry, err error) error { + if err != nil { + Exit(err) + } + // 添加文件路径到文件列表 + if IsFile(path) { + fileList = append(fileList, path) + } + return nil + }) + // 断点续传 文件大于 10M 才会分片 + resume := &upyun.MemoryRecorder{} + // 若设置为 nil,则为正常的分片上传 + yun.SetRecorder(resume) + for _, path := range fileList { + remotePath := remoteBasePath + "/" + path + err = yun.Put(&upyun.PutObjectConfig{ + Path: remotePath, + LocalPath: path, + }) + if err != nil { + log.Printf("local file: %v remote file: %v upload status: fail errmsg:%v\n", path, remotePath, err.Error()) + } else { + log.Printf("local file: %v remote file: %v upload status: success\n", path, remotePath) + } + } +} + +func Exit(err error) { + log.Println("error => " + err.Error()) + os.Exit(1) +} + +// IsDir 判断所给路径是否为文件夹 +func IsDir(path string) bool { + s, err := os.Stat(path) + if err != nil { + return false + } + return s.IsDir() +} + +// IsFile 判断所给路径是否为文件 +func IsFile(path string) bool { + return !IsDir(path) +} + +// GetEnv drone settings下的变量将会转换为PLUGIN_开头的环境变量 +func GetEnv(name string) string { + return os.Getenv("PLUGIN_" + strings.ToUpper(name)) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..208f39d --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module drone-upyun-cos + +go 1.20 + +require github.com/upyun/go-sdk/v3 v3.0.4 // indirect diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..4579a15 --- /dev/null +++ b/readme.md @@ -0,0 +1,12 @@ +# drone plugins for upyun cos + +## 参数说明 +| 参数 | 说明 | 备注 | +| ---------------- |----------|--------| +| up_operator | 操作员名称 || +| up_password | 操作员密码 || +| up_bucket | bucket名称 | 服务名称 | +| local_base_path | 本地路径 | 文件或文件夹 | +| remote_base_path | 对象存储路径 | 文件夹 | + +## 使用方式 \ No newline at end of file