|
@ -18,16 +18,15 @@ type TencentCOS struct{} |
|
|
|
|
|
|
|
|
// UploadFile upload file to COS
|
|
|
// UploadFile upload file to COS
|
|
|
func (*TencentCOS) UploadFile(file *multipart.FileHeader) (string, string, error) { |
|
|
func (*TencentCOS) UploadFile(file *multipart.FileHeader) (string, string, error) { |
|
|
c := NewClient() |
|
|
|
|
|
|
|
|
client := NewClient() |
|
|
f, openError := file.Open() |
|
|
f, openError := file.Open() |
|
|
if openError != nil { |
|
|
if openError != nil { |
|
|
global.GVA_LOG.Error("function file.Open() Filed", zap.Any("err", openError.Error())) |
|
|
global.GVA_LOG.Error("function file.Open() Filed", zap.Any("err", openError.Error())) |
|
|
|
|
|
|
|
|
return "", "", errors.New("function file.Open() Filed, err:" + openError.Error()) |
|
|
return "", "", errors.New("function file.Open() Filed, err:" + openError.Error()) |
|
|
} |
|
|
} |
|
|
fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename) |
|
|
fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename) |
|
|
|
|
|
|
|
|
_, err := c.Object.Put(context.Background(), global.GVA_CONFIG.TencentCOS.PathPrefix+"/"+fileKey, f, nil) |
|
|
|
|
|
|
|
|
_, err := client.Object.Put(context.Background(), global.GVA_CONFIG.TencentCOS.PathPrefix+"/"+fileKey, f, nil) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
panic(err) |
|
|
panic(err) |
|
|
} |
|
|
} |
|
@ -36,9 +35,9 @@ func (*TencentCOS) UploadFile(file *multipart.FileHeader) (string, string, error |
|
|
|
|
|
|
|
|
// DeleteFile delete file form COS
|
|
|
// DeleteFile delete file form COS
|
|
|
func (*TencentCOS) DeleteFile(key string) error { |
|
|
func (*TencentCOS) DeleteFile(key string) error { |
|
|
c := NewClient() |
|
|
|
|
|
|
|
|
client := NewClient() |
|
|
name := global.GVA_CONFIG.TencentCOS.PathPrefix + "/" + key |
|
|
name := global.GVA_CONFIG.TencentCOS.PathPrefix + "/" + key |
|
|
_, err := c.Object.Delete(context.Background(), name) |
|
|
|
|
|
|
|
|
_, err := client.Object.Delete(context.Background(), name) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
global.GVA_LOG.Error("function bucketManager.Delete() Filed", zap.Any("err", err.Error())) |
|
|
global.GVA_LOG.Error("function bucketManager.Delete() Filed", zap.Any("err", err.Error())) |
|
|
return errors.New("function bucketManager.Delete() Filed, err:" + err.Error()) |
|
|
return errors.New("function bucketManager.Delete() Filed, err:" + err.Error()) |
|
@ -48,13 +47,13 @@ func (*TencentCOS) DeleteFile(key string) error { |
|
|
|
|
|
|
|
|
// NewClient init COS client
|
|
|
// NewClient init COS client
|
|
|
func NewClient() *cos.Client { |
|
|
func NewClient() *cos.Client { |
|
|
u, _ := url.Parse("https://" + global.GVA_CONFIG.TencentCOS.Bucket + ".cos." + global.GVA_CONFIG.TencentCOS.Region + ".myqcloud.com") |
|
|
|
|
|
b := &cos.BaseURL{BucketURL: u} |
|
|
|
|
|
c := cos.NewClient(b, &http.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{ |
|
|
Transport: &cos.AuthorizationTransport{ |
|
|
SecretID: global.GVA_CONFIG.TencentCOS.SecretID, |
|
|
SecretID: global.GVA_CONFIG.TencentCOS.SecretID, |
|
|
SecretKey: global.GVA_CONFIG.TencentCOS.SecretKey, |
|
|
SecretKey: global.GVA_CONFIG.TencentCOS.SecretKey, |
|
|
}, |
|
|
}, |
|
|
}) |
|
|
}) |
|
|
return c |
|
|
|
|
|
|
|
|
return client |
|
|
} |
|
|
} |