forked from DreamOneX/PhoenixBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'LNSSPsd:main' into main
- Loading branch information
Showing
9 changed files
with
264 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/andybalholm/brotli" | ||
) | ||
|
||
func GetFileData(fname string) ([]byte, error) { | ||
fp, err := os.OpenFile(fname, os.O_CREATE|os.O_RDONLY, 0755) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer fp.Close() | ||
buf, err := ioutil.ReadAll(fp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return buf, err | ||
} | ||
|
||
func WriteFileData(fname string, data []byte) error { | ||
fp, err := os.OpenFile(fname, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755) | ||
if err != nil { | ||
return err | ||
} | ||
defer fp.Close() | ||
if _, err := fp.Write(data); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func CompressSingleFile(in, out string) { | ||
var origData []byte | ||
var err error | ||
in = strings.TrimSpace(in) | ||
out = strings.TrimSpace(out) | ||
origData, err = GetFileData(in) | ||
if err != nil { | ||
panic(fmt.Sprintf("read %v fail: %v", in, err)) | ||
} else if len(origData) == 0 { | ||
panic(fmt.Sprintf("read %v fail: data length = 0", in)) | ||
} | ||
|
||
buf := bytes.NewBuffer([]byte{}) | ||
compressor := brotli.NewWriterLevel(buf, brotli.DefaultCompression) | ||
compressor.Write(origData) | ||
compressor.Close() | ||
newData := buf.Bytes() | ||
|
||
if err := WriteFileData(out, newData); err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("comprerss: %v -> %v compress %.3f\n", in, out, float32(len(newData))/float32(len(origData))) | ||
} | ||
|
||
func main() { | ||
_inFile := flag.String("in", "", "input") | ||
_outFile := flag.String("out", "", "outfile") | ||
flag.Parse() | ||
inFile := strings.TrimSpace(*_inFile) | ||
outFile := strings.TrimSpace(*_outFile) | ||
fmt.Println(inFile, outFile) | ||
if strings.Contains(inFile, ",") { | ||
ins := strings.Split(inFile, ",") | ||
outs := strings.Split(outFile, ",") | ||
if len(ins) != len(outs) { | ||
panic(fmt.Errorf("%v!->%v :input/outputs mismatch", ins, outs)) | ||
} | ||
var wg sync.WaitGroup | ||
for i := range ins { | ||
wg.Add(1) | ||
in, out := strings.TrimSpace(ins[i]), strings.TrimSpace(outs[i]) | ||
go func() { | ||
CompressSingleFile(in, out) | ||
wg.Done() | ||
}() | ||
} | ||
wg.Wait() | ||
} else { | ||
CompressSingleFile(inFile, outFile) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module cos_uploader | ||
|
||
go 1.18 | ||
|
||
require ( | ||
github.com/clbanning/mxj v1.8.4 // indirect | ||
github.com/google/go-querystring v1.1.0 // indirect | ||
github.com/mitchellh/mapstructure v1.5.0 // indirect | ||
github.com/mozillazg/go-httpheader v0.3.1 // indirect | ||
github.com/tencentyun/cos-go-sdk-v5 v0.7.41 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mod h1:1pk82RBxDY/JZnPQrtqHlUFfCctgdorsd9M06fMynOM= | ||
github.com/clbanning/mxj v1.8.4 h1:HuhwZtbyvyOw+3Z1AowPkU87JkJUSv751ELWaiTpj8I= | ||
github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= | ||
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= | ||
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= | ||
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= | ||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= | ||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= | ||
github.com/mozillazg/go-httpheader v0.2.1/go.mod h1:jJ8xECTlalr6ValeXYdOF8fFUISeBAdw6E61aqQma60= | ||
github.com/mozillazg/go-httpheader v0.3.1 h1:IRP+HFrMX2SlwY9riuio7raffXUpzAosHtZu25BSJok= | ||
github.com/mozillazg/go-httpheader v0.3.1/go.mod h1:PuT8h0pw6efvp8ZeUec1Rs7dwjK08bt6gKSReGMqtdA= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= | ||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.194/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y= | ||
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.194/go.mod h1:yrBKWhChnDqNz1xuXdSbWXG56XawEq0G5j1lg4VwBD4= | ||
github.com/tencentyun/cos-go-sdk-v5 v0.7.41 h1:iU0Li/Np78H4SBna0ECQoF3mpgi6ImLXU+doGzPFXGc= | ||
github.com/tencentyun/cos-go-sdk-v5 v0.7.41/go.mod h1:4dCEtLHGh8QPxHEkgq+nFaky7yZxQuYwgSJM87icDaw= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/tencentyun/cos-go-sdk-v5" | ||
) | ||
|
||
var ( | ||
AccessUrl = "https://data-?????.cos.ap-shanghai.myqcloud.com" | ||
ServiceUrl = "https://cos.ap-??????.myqcloud.com" | ||
SecretID = "AKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | ||
SecretKey = "????????????????????????????????" | ||
) | ||
|
||
func AcquireCosClient() *cos.Client { | ||
fmt.Printf("AccessUrl: %v... ServiceUrl: %v... SecretID: %v... SecretKey: %v... \n", AccessUrl[:16], ServiceUrl[:16], SecretID[:4], SecretKey[:1]) | ||
u, _ := url.Parse(AccessUrl) | ||
su, _ := url.Parse(ServiceUrl) | ||
b := &cos.BaseURL{BucketURL: u, ServiceURL: su} | ||
client := cos.NewClient(b, &http.Client{ | ||
Transport: &cos.AuthorizationTransport{ | ||
SecretID: SecretID, | ||
SecretKey: SecretKey, | ||
}, | ||
}) | ||
return client | ||
} | ||
|
||
var localDir = flag.String("l", ".", "local dir to upload") | ||
var remoteDir = flag.String("r", "", "remote dir to upload to") | ||
|
||
func main() { | ||
flag.Parse() | ||
client := AcquireCosClient() | ||
fmt.Println(client) | ||
|
||
s, _, err := client.Service.Get(context.Background()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if len(s.Buckets) == 0 { | ||
_, err := client.Bucket.Put(context.Background(), nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
for _, b := range s.Buckets { | ||
fmt.Printf("Bucket: %#v\n", b) | ||
} | ||
localDirName := *localDir | ||
remoteDirName := *remoteDir | ||
|
||
localDirName, err = filepath.Abs(localDirName) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("dir to upload: %v -> %v \n", localDirName, remoteDirName) | ||
|
||
if err = filepath.Walk(localDirName, func(fullPath string, info os.FileInfo, err error) error { | ||
i := 0 | ||
// have to do so to avoid error remote path | ||
for i = 0; i < len(localDirName) && i < len(fullPath); i++ { | ||
if localDirName[i] != fullPath[i] { | ||
break | ||
} | ||
} | ||
fileSubPath := fullPath[i:] | ||
if len(fileSubPath) > 0 && fileSubPath[0] == '/' { | ||
fileSubPath = fileSubPath[1:] | ||
} | ||
// no need (also cannot) to upload dir | ||
if info.IsDir() { | ||
return nil | ||
} | ||
targetFilePath := fileSubPath | ||
if len(remoteDirName) > 0 && remoteDirName[len(remoteDirName)-1] != '/' { | ||
remoteDirName = strings.TrimRight(remoteDirName, "/") | ||
targetFilePath = remoteDirName + "/" + fileSubPath | ||
} | ||
fmt.Printf("uploading %v -> %v\n", fullPath, targetFilePath) | ||
_, err = client.Object.PutFromFile(context.Background(), targetFilePath, fullPath, nil) | ||
if err != nil { | ||
fmt.Printf("on uploading %v -> %v, error happen %v", fullPath, targetFilePath, err) | ||
return err | ||
} | ||
return nil | ||
}); err != nil { | ||
panic(err) | ||
} | ||
fmt.Println("Done") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
5.0.0 | ||
5.0.1 |