1
This commit is contained in:
commit
34bd21efc4
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
.*
|
||||
!.gitignore
|
||||
go.sum
|
||||
env.yml
|
||||
build
|
||||
release
|
||||
node_modules
|
||||
package.json
|
62
COS.go
Normal file
62
COS.go
Normal file
@ -0,0 +1,62 @@
|
||||
package tencent
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
sts "github.com/tencentyun/qcloud-cos-sts-sdk/go"
|
||||
)
|
||||
|
||||
func getCosClient(bucket string) *sts.Client {
|
||||
return sts.NewClient(conf.Cos[bucket].SecretId, conf.Cos[bucket].SecretKey, nil)
|
||||
}
|
||||
|
||||
type Cos struct {
|
||||
client *sts.Client
|
||||
bucket string
|
||||
appId string
|
||||
region string
|
||||
}
|
||||
|
||||
func GetCos(bucket string) *Cos {
|
||||
bConf := conf.Cos[bucket]
|
||||
a := strings.Split(bConf.Bucket, "-")
|
||||
return &Cos{
|
||||
client: getCosClient(bucket),
|
||||
bucket: bConf.Bucket,
|
||||
appId: a[len(a)-1],
|
||||
region: bConf.Region,
|
||||
}
|
||||
}
|
||||
|
||||
type CosToken struct {
|
||||
sts.Credentials
|
||||
Bucket string
|
||||
Region string
|
||||
}
|
||||
|
||||
var defaultAllowPath = "/"
|
||||
|
||||
func (c *Cos) GetToken(allowPath *string) (*CosToken, error) {
|
||||
if allowPath == nil {
|
||||
allowPath = &defaultAllowPath
|
||||
}
|
||||
crt, err := c.client.GetCredential(&sts.CredentialOptions{
|
||||
DurationSeconds: 3600,
|
||||
Region: c.region,
|
||||
Policy: &sts.CredentialPolicy{
|
||||
Statement: []sts.CredentialPolicyStatement{{
|
||||
Action: []string{"name/cos:PostObject", "name/cos:PutObject", "name/cos:InitiateMultipartUpload", "name/cos:ListMultipartUploads", "name/cos:ListParts", "name/cos:UploadPart", "name/cos:CompleteMultipartUpload"},
|
||||
Effect: "allow",
|
||||
Resource: []string{"qcs::cos:" + c.region + ":uid/" + c.appId + ":" + c.bucket + *allowPath + "*"},
|
||||
}},
|
||||
},
|
||||
})
|
||||
if err == nil {
|
||||
return &CosToken{
|
||||
Credentials: *crt.Credentials,
|
||||
Bucket: c.bucket,
|
||||
Region: c.region,
|
||||
}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
24
go.mod
Normal file
24
go.mod
Normal file
@ -0,0 +1,24 @@
|
||||
module apigo.cc/cloud/tencent
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
apigo.cc/gojs v0.0.10
|
||||
apigo.cc/gojs/console v0.0.2
|
||||
github.com/ssgo/config v1.7.9
|
||||
github.com/ssgo/u v1.7.13
|
||||
github.com/tencentyun/qcloud-cos-sts-sdk v0.0.0-20241118064430-63a76784514f
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/dlclark/regexp2 v1.11.4 // indirect
|
||||
github.com/fsnotify/fsnotify v1.8.0 // indirect
|
||||
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
|
||||
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd // indirect
|
||||
github.com/ssgo/log v1.7.7 // indirect
|
||||
github.com/ssgo/standard v1.7.7 // indirect
|
||||
github.com/ssgo/tool v0.4.27 // indirect
|
||||
golang.org/x/sys v0.28.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
68
main.go
Normal file
68
main.go
Normal file
@ -0,0 +1,68 @@
|
||||
package tencent
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
|
||||
"apigo.cc/gojs"
|
||||
"apigo.cc/gojs/goja"
|
||||
"github.com/ssgo/config"
|
||||
"github.com/ssgo/u"
|
||||
)
|
||||
|
||||
var confAes = u.NewAes([]byte("?GQ$0K0GgLdO=f+~L68PLm$uhKr4'=tV"), []byte("VFs7@sK61cj^f?HZ"))
|
||||
var keysIsSet = false
|
||||
|
||||
func SetSSKey(key, iv []byte) {
|
||||
if !keysIsSet {
|
||||
confAes = u.NewAes(key, iv)
|
||||
keysIsSet = true
|
||||
}
|
||||
}
|
||||
|
||||
var conf = struct {
|
||||
Common *struct {
|
||||
SecretId string
|
||||
SecretKey string
|
||||
}
|
||||
Cos map[string]*struct {
|
||||
Region string
|
||||
Bucket string
|
||||
SecretId string
|
||||
SecretKey string
|
||||
}
|
||||
}{}
|
||||
|
||||
func init() {
|
||||
obj := gojs.Map{
|
||||
"cos": GetCos,
|
||||
}
|
||||
|
||||
gojs.Register("apigo.cc/cloud/tencent", gojs.Module{
|
||||
ObjectMaker: func(vm *goja.Runtime) gojs.Map {
|
||||
config.LoadConfig("tencent", &conf)
|
||||
conf.Common.SecretId = confAes.DecryptUrlBase64ToString(conf.Common.SecretId)
|
||||
conf.Common.SecretKey = confAes.DecryptUrlBase64ToString(conf.Common.SecretKey)
|
||||
for _, v := range conf.Cos {
|
||||
v.SecretId, v.SecretKey = makeSecretIdAndKey(v.SecretId, v.SecretKey)
|
||||
}
|
||||
|
||||
return gojs.ToMap(obj)
|
||||
},
|
||||
TsCode: gojs.MakeTSCode(obj),
|
||||
SetSSKey: SetSSKey,
|
||||
})
|
||||
}
|
||||
|
||||
func makeSecretIdAndKey(secretId, SecretKey string) (string, string) {
|
||||
if secretId != "" {
|
||||
secretId = confAes.DecryptUrlBase64ToString(secretId)
|
||||
} else {
|
||||
secretId = conf.Common.SecretId
|
||||
}
|
||||
if SecretKey != "" {
|
||||
SecretKey = confAes.DecryptUrlBase64ToString(SecretKey)
|
||||
} else {
|
||||
SecretKey = conf.Common.SecretKey
|
||||
}
|
||||
return secretId, SecretKey
|
||||
}
|
23
main_test.go
Normal file
23
main_test.go
Normal file
@ -0,0 +1,23 @@
|
||||
package tencent_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
_ "apigo.cc/cloud/tencent"
|
||||
"apigo.cc/gojs"
|
||||
_ "apigo.cc/gojs/console"
|
||||
"github.com/ssgo/u"
|
||||
)
|
||||
|
||||
func TestHash(t *testing.T) {
|
||||
gojs.ExportForDev()
|
||||
r, err := gojs.RunFile("main_test.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if r != true {
|
||||
t.Fatal(r)
|
||||
} else {
|
||||
fmt.Println(u.BGreen("test succeess"))
|
||||
}
|
||||
}
|
11
main_test.js
Normal file
11
main_test.js
Normal file
@ -0,0 +1,11 @@
|
||||
import tencent from 'apigo.cc/cloud/tencent'
|
||||
import co from 'apigo.cc/gojs/console'
|
||||
|
||||
try {
|
||||
let lao = tencent.cos('lao')
|
||||
co.info(JSON.stringify(lao.getToken()))
|
||||
} catch (ex) {
|
||||
co.error(ex)
|
||||
}
|
||||
|
||||
return true
|
Loading…
Reference in New Issue
Block a user