-
Notifications
You must be signed in to change notification settings - Fork 61
/
cos.go
executable file
·148 lines (136 loc) · 3.33 KB
/
cos.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package CloudStore
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/tencentyun/cos-go-sdk-v5"
)
type COS struct {
AccessKey string
SecretKey string
Bucket string
AppID string
Region string
Domain string
Client *cos.Client
}
func NewCOS(accessKey, secretKey, bucket, appId, region, domain string) (c *COS, err error) {
c = &COS{
AccessKey: accessKey,
SecretKey: secretKey,
Bucket: bucket,
AppID: appId,
Region: region,
}
u, _ := url.Parse(fmt.Sprintf("https://%v-%v.cos.%v.myqcloud.com", bucket, appId, region))
if domain == "" {
domain = u.String()
}
c.Domain = strings.TrimRight(domain, "/ ")
c.Client = cos.NewClient(
&cos.BaseURL{BucketURL: u},
&http.Client{
Timeout: 1800 * time.Second,
Transport: &cos.AuthorizationTransport{
SecretID: accessKey,
SecretKey: secretKey,
},
})
return
}
func (c *COS) IsExist(object string) (err error) {
_, err = c.GetInfo(object)
return
}
func (c *COS) Upload(tmpFile, saveFile string, headers ...map[string]string) (err error) {
var reader *os.File
reader, err = os.Open(tmpFile)
if err != nil {
return
}
defer reader.Close()
objHeader := &cos.ObjectPutHeaderOptions{}
for _, header := range headers {
for k, v := range header {
switch strings.ToLower(k) {
case "content-encoding":
objHeader.ContentEncoding = v
case "content-type":
objHeader.ContentType = v
case "content-disposition":
objHeader.ContentDisposition = v
}
}
}
opt := &cos.ObjectPutOptions{ObjectPutHeaderOptions: objHeader}
_, err = c.Client.Object.Put(context.Background(), objectRel(saveFile), reader, opt)
return
}
func (c *COS) Delete(objects ...string) (err error) {
var errs []string
for _, object := range objects {
_, err = c.Client.Object.Delete(context.Background(), objectRel(object))
if err != nil {
errs = append(errs, err.Error())
}
}
if len(errs) > 0 {
err = errors.New(strings.Join(errs, "; "))
}
return
}
func (c *COS) GetSignURL(object string, expire int64) (link string, err error) {
if expire <= 0 {
link = c.Domain + objectAbs(object)
return
}
var u *url.URL
exp := time.Duration(expire) * time.Second
u, err = c.Client.Object.GetPresignedURL(context.Background(),
http.MethodGet, objectRel(object),
c.AccessKey, c.SecretKey,
exp, nil)
if err != nil {
return
}
link = u.String()
if !strings.HasPrefix(link, c.Domain) {
link = c.Domain + u.RequestURI()
}
return
}
func (c *COS) Download(object string, savePath string) (err error) {
_, err = c.Client.Object.GetToFile(context.Background(), objectRel(object), savePath, nil)
return
}
func (c *COS) GetInfo(object string) (info File, err error) {
var resp *cos.Response
path := objectRel(object)
resp, err = c.Client.Object.Get(context.Background(), path, nil)
if err != nil {
return
}
defer resp.Body.Close()
header := make(map[string]string)
for k, _ := range resp.Header {
header[k] = resp.Header.Get(k)
}
info = File{
Header: header,
Name: path,
}
info.ModTime, _ = time.Parse(http.TimeFormat, resp.Header.Get("Last-Modified"))
info.Size, _ = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
info.IsDir = info.Size == 0
return
}
func (c *COS) Lists(prefix string) (files []File, err error) {
// TODO: 腾讯云的SDK中暂时没开放这个功能
return
}