feat: add doge driver (#6201)

* feat: add doge driver

* doc: 补充readme文档

* fix: 对齐meta信息

* fix: 调整结构体名字,与driver保持一致

* perf: merge to s3

* Rename goge.go to doge.go

---------

Co-authored-by: Andy Hsu <i@nn.ci>
This commit is contained in:
二丫讲梵
2024-03-25 22:53:44 +08:00
committed by GitHub
parent 9c84b6596f
commit cf08aa3668
10 changed files with 141 additions and 27 deletions

62
drivers/s3/doge.go Normal file
View File

@ -0,0 +1,62 @@
package s3
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"io"
"net/http"
"strings"
)
type TmpTokenResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data TmpTokenResponseData `json:"data,omitempty"`
}
type TmpTokenResponseData struct {
Credentials Credentials `json:"Credentials"`
}
type Credentials struct {
AccessKeyId string `json:"accessKeyId,omitempty"`
SecretAccessKey string `json:"secretAccessKey,omitempty"`
SessionToken string `json:"sessionToken,omitempty"`
}
func getCredentials(AccessKey, SecretKey string) (rst Credentials, err error) {
apiPath := "/auth/tmp_token.json"
reqBody, err := json.Marshal(map[string]interface{}{"channel": "OSS_FULL", "scopes": []string{"*"}})
if err != nil {
return rst, err
}
signStr := apiPath + "\n" + string(reqBody)
hmacObj := hmac.New(sha1.New, []byte(SecretKey))
hmacObj.Write([]byte(signStr))
sign := hex.EncodeToString(hmacObj.Sum(nil))
Authorization := "TOKEN " + AccessKey + ":" + sign
req, err := http.NewRequest("POST", "https://api.dogecloud.com"+apiPath, strings.NewReader(string(reqBody)))
if err != nil {
return rst, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", Authorization)
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return rst, err
}
defer resp.Body.Close()
ret, err := io.ReadAll(resp.Body)
if err != nil {
return rst, err
}
var tmpTokenResp TmpTokenResponse
err = json.Unmarshal(ret, &tmpTokenResp)
if err != nil {
return rst, err
}
return tmpTokenResp.Data.Credentials, nil
}

View File

@ -26,10 +26,12 @@ type S3 struct {
Session *session.Session
client *s3.S3
linkClient *s3.S3
config driver.Config
}
func (d *S3) Config() driver.Config {
return config
return d.config
}
func (d *S3) GetAddition() driver.Additional {

View File

@ -22,15 +22,25 @@ type Addition struct {
AddFilenameToDisposition bool `json:"add_filename_to_disposition" help:"Add filename to Content-Disposition header."`
}
var config = driver.Config{
Name: "S3",
DefaultRoot: "/",
LocalSort: true,
CheckStatus: true,
}
func init() {
op.RegisterDriver(func() driver.Driver {
return &S3{}
return &S3{
config: driver.Config{
Name: "S3",
DefaultRoot: "/",
LocalSort: true,
CheckStatus: true,
},
}
})
op.RegisterDriver(func() driver.Driver {
return &S3{
config: driver.Config{
Name: "Doge",
DefaultRoot: "/",
LocalSort: true,
CheckStatus: true,
},
}
})
}

View File

@ -21,13 +21,21 @@ import (
// do others that not defined in Driver interface
func (d *S3) initSession() error {
var err error
accessKeyID, secretAccessKey, sessionToken := d.AccessKeyID, d.SecretAccessKey, d.SessionToken
if d.config.Name == "Doge" {
credentialsTmp, err := getCredentials(d.AccessKeyID, d.SecretAccessKey)
if err != nil {
return err
}
accessKeyID, secretAccessKey, sessionToken = credentialsTmp.AccessKeyId, credentialsTmp.SecretAccessKey, credentialsTmp.SessionToken
}
cfg := &aws.Config{
Credentials: credentials.NewStaticCredentials(d.AccessKeyID, d.SecretAccessKey, d.SessionToken),
Credentials: credentials.NewStaticCredentials(accessKeyID, secretAccessKey, sessionToken),
Region: &d.Region,
Endpoint: &d.Endpoint,
S3ForcePathStyle: aws.Bool(d.ForcePathStyle),
}
var err error
d.Session, err = session.NewSession(cfg)
return err
}