forked from lilith44/uoa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cos.go
239 lines (201 loc) · 6.37 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package uoa
import (
`context`
`crypto/tls`
`encoding/json`
`fmt`
`net/http`
`net/url`
`strings`
`sync`
`time`
`github.com/storezhang/gox`
`github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common`
`github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile`
sts `github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sts/v20180813`
`github.com/tencentyun/cos-go-sdk-v5`
)
var _ uoaInternal = (*cosInternal)(nil)
// cosInternal 腾讯云存储
type cosInternal struct {
clientCache sync.Map
}
func (c *cosInternal) url(ctx context.Context, key string, filename string, options *urlOptions) (url *url.URL, err error) {
var (
client *cos.Client
getOptions *cos.ObjectGetOptions
headRsp *cos.Response
contentType string
)
if client, err = c.getClient(options.endpoint, options.secret); nil != err {
return
}
// 检查文件是否存在,文件不存在没必要往下继续执行
if headRsp, err = client.Object.Head(ctx, key, nil); nil != err {
if rspErr, ok := err.(*cos.ErrorResponse); ok && http.StatusNotFound == rspErr.Response.StatusCode {
err = nil
}
return
}
if options.download {
getOptions = &cos.ObjectGetOptions{
ResponseContentDisposition: gox.ContentDisposition(filename, gox.ContentDispositionTypeAttachment),
}
} else if options.inline {
if "" != options.contentType {
contentType = options.contentType
} else {
contentType = headRsp.Header.Get(gox.HeaderContentType)
}
getOptions = &cos.ObjectGetOptions{
ResponseContentDisposition: gox.ContentDisposition(filename, gox.ContentDispositionTypeInline),
ResponseContentType: contentType,
}
}
// 获取预签名URL
if url, err = client.Object.GetPresignedURL(
ctx,
http.MethodGet,
key,
options.secret.Id, options.secret.Key,
options.expired,
getOptions,
); nil != err {
return
}
return
}
func (c *cosInternal) initiateMultipartUpload(ctx context.Context, key string, options *multipartOptions) (uploadId string, err error) {
var client *cos.Client
if client, err = c.getClient(options.endpoint, options.secret); nil != err {
return
}
var rsp *cos.InitiateMultipartUploadResult
if rsp, _, err = client.Object.InitiateMultipartUpload(ctx, key, nil); nil != err {
return
}
uploadId = rsp.UploadID
return
}
func (c *cosInternal) completeMultipartUpload(ctx context.Context, key string, uploadId string, parts []cos.Object, options *multipartOptions) (err error) {
var client *cos.Client
if client, err = c.getClient(options.endpoint, options.secret); nil != err {
return
}
opt := &cos.CompleteMultipartUploadOptions{Parts: parts}
_, _, err = client.Object.CompleteMultipartUpload(ctx, key, uploadId, opt)
return
}
func (c *cosInternal) abortMultipartUpload(ctx context.Context, key string, uploadId string, options *multipartOptions) (err error) {
var client *cos.Client
if client, err = c.getClient(options.endpoint, options.secret); nil != err {
return
}
_, err = client.Object.AbortMultipartUpload(ctx, key, uploadId)
return
}
func (c *cosInternal) credentials(_ context.Context, options *credentialsOptions, keys ...string) (credentials *credentialsBase, err error) {
actions := []string{
// 简单上传
"name/cos:PutObject",
// 表单上传、小程序上传
"name/cos:PostObject",
// 分块上传:初始化分块操作
"name/cos:InitiateMultipartUpload",
// 分块上传:列举进行中的分块上传
"name/cos:ListMultipartUploads",
// 分块上传:列举已上传分块操作
"name/cos:ListParts",
// 分块上传:上传分块块操作
"name/cos:UploadPart",
// 分块上传:完成所有分块上传操作
"name/cos:CompleteMultipartUpload",
// 取消分块上传操作
"name/cos:AbortMultipartUpload",
}
region, appId, bucketName := c.parse(options.endpoint)
resources := make([]string, 0, len(keys))
for _, key := range keys {
resources = append(resources, fmt.Sprintf("qcs::cos:%s:uid/%s:%s/%s", region, appId, bucketName, key))
}
policy := cosPolicy{
Version: options.version,
Statements: []cosStatement{
{
Actions: actions,
Effect: "allow",
Resources: resources,
},
},
}
var policyBytes []byte
if policyBytes, err = json.Marshal(policy); nil != err {
return
}
credential := common.NewCredential(options.secret.Id, options.secret.Key)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = options.url
client, _ := sts.NewClient(credential, region, cpf)
req := sts.NewGetFederationTokenRequest()
req.Name = common.StringPtr("cos-credential-go")
req.Policy = common.StringPtr(string(policyBytes))
req.DurationSeconds = common.Uint64Ptr(uint64(options.expired / time.Second))
var rsp *sts.GetFederationTokenResponse
if rsp, err = client.GetFederationToken(req); nil != err {
return
}
credentials = &credentialsBase{
Id: *rsp.Response.Credentials.TmpSecretId,
Key: *rsp.Response.Credentials.TmpSecretKey,
Token: *rsp.Response.Credentials.Token,
Expired: time.Unix(int64(*rsp.Response.ExpiredTime), 0),
}
return
}
func (c *cosInternal) delete(ctx context.Context, key string, options *deleteOptions) (err error) {
var client *cos.Client
if client, err = c.getClient(options.endpoint, options.secret); nil != err {
return
}
opts := make([]*cos.ObjectDeleteOptions, 0, 0)
if "" != options.version {
opts = append(opts, &cos.ObjectDeleteOptions{
VersionId: options.version,
})
}
_, err = client.Object.Delete(ctx, key, opts...)
return
}
func (c *cosInternal) getClient(baseUrl string, secret gox.Secret) (client *cos.Client, err error) {
var (
cache interface{}
ok bool
)
key := fmt.Sprintf("%s-%s", baseUrl, secret.Id)
if cache, ok = c.clientCache.Load(key); ok {
client = cache.(*cos.Client)
return
}
var bucketUrl *url.URL
if bucketUrl, err = url.Parse(baseUrl); nil != err {
return
}
client = cos.NewClient(&cos.BaseURL{BucketURL: bucketUrl}, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: secret.Id,
SecretKey: secret.Key,
// nolint:gosec
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
},
})
c.clientCache.Store(key, client)
return
}
func (c *cosInternal) parse(endpoint string) (region string, appId string, bucketName string) {
endpoint = strings.ReplaceAll(endpoint, "https://", "")
urls := strings.Split(endpoint, ".")
region = urls[2]
bucketName = urls[0]
appId = strings.Split(urls[0], "-")[1]
return
}