-
Notifications
You must be signed in to change notification settings - Fork 14
/
cos.go
176 lines (131 loc) · 4.31 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
package filesystem
import (
"bytes"
"context"
"fmt"
"github.com/iimeta/iim-client/internal/config"
"github.com/iimeta/iim-client/utility/util"
"mime/multipart"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"time"
"github.com/tencentyun/cos-go-sdk-v5"
)
type CosFilesystem struct {
conf *config.Config
client *cos.Client
}
// NewCosFilesystem ...
// See: https://cloud.tencent.com/document/product/436/31215
func NewCosFilesystem(conf *config.Config) *CosFilesystem {
bucketURL, _ := url.Parse(fmt.Sprintf("https://%s.cos.%s.myqcloud.com", conf.Filesystem.Cos.Bucket, conf.Filesystem.Cos.Region))
client := cos.NewClient(&cos.BaseURL{
BucketURL: bucketURL,
}, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: conf.Filesystem.Cos.SecretId,
SecretKey: conf.Filesystem.Cos.SecretKey,
},
})
return &CosFilesystem{conf, client}
}
// Write 文件写入
func (c *CosFilesystem) Write(data []byte, filePath string) error {
_, err := c.client.Object.Put(context.TODO(), filePath, bytes.NewBuffer(data), nil)
return err
}
// WriteLocal 本地文件上传
func (c *CosFilesystem) WriteLocal(localFile string, filePath string) error {
_, _, err := c.client.Object.Upload(context.TODO(), filePath, localFile, nil)
return err
}
func (c *CosFilesystem) WriteFromFile(file *multipart.FileHeader, filePath string) error {
return nil
}
// Copy 文件拷贝
func (c *CosFilesystem) Copy(srcPath, filePath string) error {
sourceURL := fmt.Sprintf("%s/%s", c.client.BaseURL.BucketURL.Host, srcPath)
_, _, err := c.client.Object.Copy(context.TODO(), filePath, sourceURL, nil)
return err
}
// Delete 删除一个文件或空文件夹
func (c *CosFilesystem) Delete(filePath string) error {
_, err := c.client.Object.Delete(context.TODO(), filePath, nil)
return err
}
// DeleteDir 删除文件夹
func (c *CosFilesystem) DeleteDir(path string) error {
path = strings.TrimSuffix(path, "/") + "/"
return c.Delete(path)
}
// CreateDir 递归创建文件夹
func (c *CosFilesystem) CreateDir(path string) error {
path = strings.TrimSuffix(path, "/") + "/"
_, err := c.client.Object.Put(context.TODO(), path, strings.NewReader(""), nil)
return err
}
// Stat 文件信息
func (c *CosFilesystem) Stat(filePath string) (*FileStat, error) {
resp, err := c.client.Object.Head(context.TODO(), filePath, nil)
if err != nil {
return nil, err
}
size, _ := strconv.Atoi(resp.Header.Get("Content-Length"))
t, _ := time.ParseInLocation(time.RFC1123, resp.Header.Get("Last-Modified"), util.Location())
return &FileStat{
Name: path.Base(filePath),
Size: int64(size),
Ext: path.Ext(filePath),
LastModTime: t.Add(8 * time.Hour),
MimeType: resp.Header.Get("Content-Type"),
}, nil
}
func (c *CosFilesystem) Append(filePath string) {
}
func (c *CosFilesystem) PublicUrl(filePath string) string {
return c.client.Object.GetObjectURL(filePath).String()
}
func (c *CosFilesystem) PrivateUrl(filePath string, timeout time.Duration) string {
result, err := c.client.Object.GetPresignedURL(
context.TODO(),
http.MethodGet, filePath,
c.conf.Filesystem.Cos.SecretId,
c.conf.Filesystem.Cos.SecretKey,
timeout,
nil,
)
if err != nil {
return ""
}
return result.String()
}
// ReadStream 读取文件流信息
func (c *CosFilesystem) ReadStream(filePath string) ([]byte, error) {
return nil, nil
}
func (c *CosFilesystem) InitiateMultipartUpload(filePath string, fileName string) (string, error) {
opt := &cos.InitiateMultipartUploadOptions{
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
ContentDisposition: fmt.Sprintf("attachment; filename=\"%s\"", fileName),
},
}
resp, _, err := c.client.Object.InitiateMultipartUpload(context.TODO(), filePath, opt)
if err != nil {
return "", err
}
return resp.UploadID, nil
}
func (c *CosFilesystem) UploadPart(filePath string, uploadID string, num int, stream []byte) (string, error) {
resp, err := c.client.Object.UploadPart(context.TODO(), filePath, uploadID, num, bytes.NewBuffer(stream), nil)
if err != nil {
return "", err
}
return strings.Trim(resp.Header.Get("ETag"), `"`), nil
}
func (c *CosFilesystem) CompleteMultipartUpload(filePath string, uploadID string, opt *cos.CompleteMultipartUploadOptions) error {
_, _, err := c.client.Object.CompleteMultipartUpload(context.TODO(), filePath, uploadID, opt)
return err
}