You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.8 KiB
59 lines
1.8 KiB
package upload
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"gin-vue-admin/global"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/tencentyun/cos-go-sdk-v5"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type TencentCOS struct{}
|
|
|
|
// UploadFile upload file to COS
|
|
func (*TencentCOS) UploadFile(file *multipart.FileHeader) (string, string, error) {
|
|
client := NewClient()
|
|
f, openError := file.Open()
|
|
if openError != nil {
|
|
global.GVA_LOG.Error("function file.Open() Filed", zap.Any("err", openError.Error()))
|
|
return "", "", errors.New("function file.Open() Filed, err:" + openError.Error())
|
|
}
|
|
fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename)
|
|
|
|
_, err := client.Object.Put(context.Background(), global.GVA_CONFIG.TencentCOS.PathPrefix+"/"+fileKey, f, nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return global.GVA_CONFIG.TencentCOS.BaseURL + "/" + global.GVA_CONFIG.TencentCOS.PathPrefix + "/" + fileKey, fileKey, nil
|
|
}
|
|
|
|
// DeleteFile delete file form COS
|
|
func (*TencentCOS) DeleteFile(key string) error {
|
|
client := NewClient()
|
|
name := global.GVA_CONFIG.TencentCOS.PathPrefix + "/" + key
|
|
_, err := client.Object.Delete(context.Background(), name)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("function bucketManager.Delete() Filed", zap.Any("err", err.Error()))
|
|
return errors.New("function bucketManager.Delete() Filed, err:" + err.Error())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// NewClient init COS client
|
|
func NewClient() *cos.Client {
|
|
urlStr, _ := url.Parse("https://" + global.GVA_CONFIG.TencentCOS.Bucket + ".cos." + global.GVA_CONFIG.TencentCOS.Region + ".myqcloud.com")
|
|
baseURL := &cos.BaseURL{BucketURL: urlStr}
|
|
client := cos.NewClient(baseURL, &http.Client{
|
|
Transport: &cos.AuthorizationTransport{
|
|
SecretID: global.GVA_CONFIG.TencentCOS.SecretID,
|
|
SecretKey: global.GVA_CONFIG.TencentCOS.SecretKey,
|
|
},
|
|
})
|
|
return client
|
|
}
|