2023-04-03 10:51:11 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2023-11-02 06:29:46 +00:00
|
|
|
"os"
|
2023-04-03 10:51:11 +00:00
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
cosSDK "github.com/tencentyun/cos-go-sdk-v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
type cosClient struct {
|
|
|
|
region string
|
|
|
|
accessKey string
|
|
|
|
secretKey string
|
2023-06-27 10:57:59 +00:00
|
|
|
scType string
|
2023-04-03 10:51:11 +00:00
|
|
|
Vars map[string]interface{}
|
|
|
|
client *cosSDK.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCosClient(vars map[string]interface{}) (*cosClient, error) {
|
|
|
|
var accessKey string
|
|
|
|
var secretKey string
|
2023-06-27 10:57:59 +00:00
|
|
|
var scType string
|
2023-04-03 10:51:11 +00:00
|
|
|
var region string
|
|
|
|
if _, ok := vars["region"]; ok {
|
|
|
|
region = vars["region"].(string)
|
|
|
|
} else {
|
|
|
|
return nil, constant.ErrInvalidParams
|
|
|
|
}
|
|
|
|
if _, ok := vars["accessKey"]; ok {
|
|
|
|
accessKey = vars["accessKey"].(string)
|
|
|
|
} else {
|
|
|
|
return nil, constant.ErrInvalidParams
|
|
|
|
}
|
2023-06-27 10:57:59 +00:00
|
|
|
if _, ok := vars["scType"]; ok {
|
|
|
|
scType = vars["scType"].(string)
|
|
|
|
} else {
|
|
|
|
scType = "Standard"
|
|
|
|
}
|
2023-04-03 10:51:11 +00:00
|
|
|
if _, ok := vars["secretKey"]; ok {
|
|
|
|
secretKey = vars["secretKey"].(string)
|
|
|
|
} else {
|
|
|
|
return nil, constant.ErrInvalidParams
|
|
|
|
}
|
|
|
|
|
|
|
|
u, _ := url.Parse(fmt.Sprintf("https://cos.%s.myqcloud.com", region))
|
|
|
|
b := &cosSDK.BaseURL{BucketURL: u}
|
|
|
|
client := cosSDK.NewClient(b, &http.Client{
|
|
|
|
Transport: &cosSDK.AuthorizationTransport{
|
|
|
|
SecretID: accessKey,
|
|
|
|
SecretKey: secretKey,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-06-27 10:57:59 +00:00
|
|
|
return &cosClient{Vars: vars, client: client, accessKey: accessKey, secretKey: secretKey, scType: scType, region: region}, nil
|
2023-04-03 10:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cos cosClient) ListBuckets() ([]interface{}, error) {
|
|
|
|
buckets, _, err := cos.client.Service.Get(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var datas []interface{}
|
|
|
|
for _, bucket := range buckets.Buckets {
|
2023-04-05 13:58:28 +00:00
|
|
|
datas = append(datas, bucket.Name)
|
2023-04-03 10:51:11 +00:00
|
|
|
}
|
|
|
|
return datas, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cos cosClient) Exist(path string) (bool, error) {
|
|
|
|
client, err := cos.newClientWithBucket()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-05-12 14:30:26 +00:00
|
|
|
exist, err := client.Object.IsExist(context.Background(), path)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2023-04-03 10:51:11 +00:00
|
|
|
}
|
2023-05-12 14:30:26 +00:00
|
|
|
return exist, nil
|
2023-04-03 10:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cos cosClient) Delete(path string) (bool, error) {
|
|
|
|
client, err := cos.newClientWithBucket()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if _, err := client.Object.Delete(context.Background(), path); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cos cosClient) Upload(src, target string) (bool, error) {
|
|
|
|
client, err := cos.newClientWithBucket()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-11-02 06:29:46 +00:00
|
|
|
fileInfo, err := os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if fileInfo.Size() > 5368709120 {
|
|
|
|
opt := &cosSDK.MultiUploadOptions{
|
|
|
|
OptIni: &cosSDK.InitiateMultipartUploadOptions{
|
|
|
|
ACLHeaderOptions: nil,
|
|
|
|
ObjectPutHeaderOptions: &cosSDK.ObjectPutHeaderOptions{
|
|
|
|
XCosStorageClass: cos.scType,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
PartSize: 200,
|
|
|
|
}
|
|
|
|
if _, _, err := client.Object.MultiUpload(
|
|
|
|
context.Background(), target, src, opt,
|
|
|
|
); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
2023-06-27 10:57:59 +00:00
|
|
|
if _, err := client.Object.PutFromFile(context.Background(), target, src, &cosSDK.ObjectPutOptions{
|
|
|
|
ACLHeaderOptions: nil,
|
|
|
|
ObjectPutHeaderOptions: &cosSDK.ObjectPutHeaderOptions{
|
|
|
|
XCosStorageClass: cos.scType,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
2023-04-03 10:51:11 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cos cosClient) Download(src, target string) (bool, error) {
|
|
|
|
client, err := cos.newClientWithBucket()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if _, err := client.Object.Download(context.Background(), src, target, &cosSDK.MultiDownloadOptions{}); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cos *cosClient) GetBucket() (string, error) {
|
|
|
|
if _, ok := cos.Vars["bucket"]; ok {
|
|
|
|
return cos.Vars["bucket"].(string), nil
|
|
|
|
} else {
|
|
|
|
return "", constant.ErrInvalidParams
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 08:44:12 +00:00
|
|
|
func (cos cosClient) ListObjects(prefix string) ([]string, error) {
|
2023-04-03 10:51:11 +00:00
|
|
|
client, err := cos.newClientWithBucket()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
datas, _, err := client.Bucket.Get(context.Background(), &cosSDK.BucketGetOptions{Prefix: prefix})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-08 08:44:12 +00:00
|
|
|
var result []string
|
2023-04-03 10:51:11 +00:00
|
|
|
for _, item := range datas.Contents {
|
|
|
|
result = append(result, item.Key)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cos cosClient) newClientWithBucket() (*cosSDK.Client, error) {
|
|
|
|
bucket, err := cos.GetBucket()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
u, _ := url.Parse(fmt.Sprintf("https://%s.cos.%s.myqcloud.com", bucket, cos.region))
|
|
|
|
b := &cosSDK.BaseURL{BucketURL: u}
|
|
|
|
client := cosSDK.NewClient(b, &http.Client{
|
|
|
|
Transport: &cosSDK.AuthorizationTransport{
|
|
|
|
SecretID: cos.accessKey,
|
|
|
|
SecretKey: cos.secretKey,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return client, nil
|
|
|
|
}
|