mirror of
https://github.com/xiaoqidun/gocos.git
synced 2024-11-23 23:06:44 +08:00
114 lines
2.3 KiB
Go
114 lines
2.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"github.com/tencentyun/cos-go-sdk-v5"
|
||
|
"io/fs"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
CodeErr = 1
|
||
|
SecretID = "secret_id"
|
||
|
SecretKey = "secret_key"
|
||
|
BucketURL = "bucket_url"
|
||
|
Source = "source"
|
||
|
Target = "target"
|
||
|
StripPrefix = "strip_prefix"
|
||
|
PathSeparator = "/"
|
||
|
)
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
var (
|
||
|
err error
|
||
|
secretID = GetConfig(SecretID)
|
||
|
secretKey = GetConfig(SecretKey)
|
||
|
bucketURL = GetConfig(BucketURL)
|
||
|
source = GetConfig(Source)
|
||
|
target = GetConfig(Target)
|
||
|
stripPrefix = GetConfig(StripPrefix)
|
||
|
)
|
||
|
if !VarIsEmpty(secretID, secretKey, bucketURL, source, target) {
|
||
|
os.Exit(CodeErr)
|
||
|
return
|
||
|
}
|
||
|
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 {
|
||
|
os.Exit(CodeErr)
|
||
|
return
|
||
|
}
|
||
|
sourceLen := len(sourceFiles)
|
||
|
if sourceLen == 0 {
|
||
|
return
|
||
|
}
|
||
|
if !strings.HasSuffix(target, PathSeparator) {
|
||
|
target += PathSeparator
|
||
|
}
|
||
|
u, err := url.Parse(bucketURL)
|
||
|
if err != nil {
|
||
|
os.Exit(CodeErr)
|
||
|
return
|
||
|
}
|
||
|
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 {
|
||
|
os.Exit(CodeErr)
|
||
|
return
|
||
|
}
|
||
|
log.Printf("source:%s target:%s\r\n", local, remote)
|
||
|
}
|
||
|
}
|