init: Complete plug-in coding

This commit is contained in:
rainerosion 2023-04-23 17:56:29 +08:00
commit 61e08d0060
3 changed files with 94 additions and 0 deletions

77
cos.go Normal file
View File

@ -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))
}

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module drone-upyun-cos
go 1.20
require github.com/upyun/go-sdk/v3 v3.0.4 // indirect

12
readme.md Normal file
View File

@ -0,0 +1,12 @@
# drone plugins for upyun cos
## 参数说明
| 参数 | 说明 | 备注 |
| ---------------- |----------|--------|
| up_operator | 操作员名称 ||
| up_password | 操作员密码 ||
| up_bucket | bucket名称 | 服务名称 |
| local_base_path | 本地路径 | 文件或文件夹 |
| remote_base_path | 对象存储路径 | 文件夹 |
## 使用方式