2020-12-24 10:10:59 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-24 11:49:24 +08:00
|
|
|
"errors"
|
2020-12-24 10:10:59 +08:00
|
|
|
"github.com/tencentyun/cos-go-sdk-v5"
|
|
|
|
"io/fs"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-12-26 10:52:32 +08:00
|
|
|
"time"
|
2020-12-24 15:10:34 +08:00
|
|
|
_ "time/tzdata"
|
2020-12-24 10:10:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SecretID = "secret_id"
|
|
|
|
SecretKey = "secret_key"
|
|
|
|
BucketURL = "bucket_url"
|
|
|
|
Source = "source"
|
|
|
|
Target = "target"
|
|
|
|
StripPrefix = "strip_prefix"
|
|
|
|
PathSeparator = "/"
|
|
|
|
)
|
|
|
|
|
2020-12-24 11:49:24 +08:00
|
|
|
func ErrExit(err error) {
|
|
|
|
log.Println(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2020-12-24 10:10:59 +08:00
|
|
|
func GetConfig(key string) string {
|
|
|
|
key = "PLUGIN_" + strings.ToUpper(key)
|
|
|
|
return os.Getenv(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func VarIsEmpty(a ...interface{}) bool {
|
|
|
|
for _, v := range a {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case string:
|
|
|
|
if "" == v {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case []byte:
|
|
|
|
if 0 == len(v) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case []string:
|
|
|
|
if 0 == len(v) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-26 10:52:32 +08:00
|
|
|
func init() {
|
|
|
|
tz := os.Getenv("TZ")
|
|
|
|
if tz == "" {
|
|
|
|
tz = "Asia/Shanghai"
|
|
|
|
}
|
|
|
|
time.Local, _ = time.LoadLocation(tz)
|
|
|
|
}
|
|
|
|
|
2020-12-24 10:10:59 +08:00
|
|
|
func main() {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
secretID = GetConfig(SecretID)
|
|
|
|
secretKey = GetConfig(SecretKey)
|
|
|
|
bucketURL = GetConfig(BucketURL)
|
|
|
|
source = GetConfig(Source)
|
|
|
|
target = GetConfig(Target)
|
|
|
|
stripPrefix = GetConfig(StripPrefix)
|
|
|
|
)
|
2020-12-24 11:57:50 +08:00
|
|
|
if VarIsEmpty(secretID, secretKey, bucketURL, source, target) {
|
2020-12-24 11:49:24 +08:00
|
|
|
ErrExit(errors.New("input error"))
|
2020-12-24 10:10:59 +08:00
|
|
|
}
|
|
|
|
sourceFiles := make([]string, 0, 0)
|
|
|
|
if err = filepath.WalkDir(source, func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if !d.IsDir() {
|
|
|
|
sourceFiles = append(sourceFiles, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2020-12-24 11:49:24 +08:00
|
|
|
ErrExit(err)
|
2020-12-24 10:10:59 +08:00
|
|
|
}
|
|
|
|
sourceLen := len(sourceFiles)
|
|
|
|
if sourceLen == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(target, PathSeparator) {
|
|
|
|
target += PathSeparator
|
|
|
|
}
|
|
|
|
u, err := url.Parse(bucketURL)
|
|
|
|
if err != nil {
|
2020-12-24 11:49:24 +08:00
|
|
|
ErrExit(err)
|
2020-12-24 10:10:59 +08:00
|
|
|
}
|
|
|
|
cosClient := cos.NewClient(
|
|
|
|
&cos.BaseURL{
|
|
|
|
BucketURL: u,
|
|
|
|
},
|
|
|
|
&http.Client{
|
|
|
|
Transport: &cos.AuthorizationTransport{
|
|
|
|
SecretID: secretID,
|
|
|
|
SecretKey: secretKey,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
for i := 0; i < sourceLen; i++ {
|
|
|
|
local := sourceFiles[i]
|
|
|
|
remote := strings.TrimPrefix(local, stripPrefix)
|
|
|
|
if strings.HasPrefix(remote, PathSeparator) {
|
|
|
|
remote = strings.TrimPrefix(remote, PathSeparator)
|
|
|
|
}
|
|
|
|
remote = target + remote
|
|
|
|
if _, err = cosClient.Object.PutFromFile(context.Background(), remote, local, &cos.ObjectPutOptions{
|
|
|
|
ACLHeaderOptions: &cos.ACLHeaderOptions{},
|
|
|
|
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{},
|
|
|
|
}); err != nil {
|
2020-12-24 11:49:24 +08:00
|
|
|
ErrExit(err)
|
2020-12-24 10:10:59 +08:00
|
|
|
}
|
|
|
|
log.Printf("source:%s target:%s\r\n", local, remote)
|
|
|
|
}
|
|
|
|
}
|