forked from go-admin-team/go-admin-core
/
tencent.go
79 lines (66 loc) · 1.62 KB
/
tencent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package file_store
import (
"context"
"fmt"
"github.com/tencentyun/cos-go-sdk-v5"
"net/http"
"net/url"
)
var _tencentFile *cos.Client
func GetTencentFileClient() *cos.Client {
return _tencentFile
}
type TencentFileClient struct {
client *cos.Client
accessId string
}
func NewTencentFile(client *cos.Client, accessKey, secretKey, cosUrl, ciUrl string) *TencentFileClient {
if client == nil {
cosURL, err := url.Parse(cosUrl)
if err != nil {
panic(fmt.Sprintf("Tencent file store init error: %s", err.Error()))
}
ciURL, err := url.Parse(ciUrl)
if err != nil {
panic(fmt.Sprintf("Tencent file store init error: %s", err.Error()))
}
base := &cos.BaseURL{
BucketURL: cosURL,
CIURL: ciURL,
}
transport := &cos.AuthorizationTransport{
SecretID: accessKey,
SecretKey: secretKey,
Transport: &http.Transport{},
}
client = cos.NewClient(base, &http.Client{
Transport: transport,
})
_tencentFile = client
}
r := &TencentFileClient{
client: client,
accessId: accessKey,
}
return r
}
func (rc *TencentFileClient) String() string {
return rc.accessId
}
func (rc *TencentFileClient) Check() bool {
return rc.client != nil
}
func (rc *TencentFileClient) Close() {
}
func (rc *TencentFileClient) Upload(ctx context.Context, name, fileLocation string) (string, error) {
response, err := rc.client.Object.PutFromFile(ctx, name, fileLocation, nil)
if err != nil {
return fileLocation, err
}
defer response.Body.Close()
return response.Response.Request.URL.String(), nil
}
// GetClient 暴露原生client
func (rc *TencentFileClient) GetClient() interface{} {
return rc.client
}