forked from TruthHun/CloudStore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bos.go
executable file
·136 lines (124 loc) · 3.07 KB
/
bos.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
package CloudStore
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/baidubce/bce-sdk-go/services/bos"
"github.com/baidubce/bce-sdk-go/services/bos/api"
)
type BOS struct {
AccessKey string
SecretKey string
Bucket string
Endpoint string
Domain string
Client *bos.Client
}
// new bos
func NewBOS(accessKey, secretKey, bucket, endpoint, domain string) (b *BOS, err error) {
b = &BOS{
AccessKey: accessKey,
SecretKey: secretKey,
Bucket: bucket,
Endpoint: endpoint,
}
if domain == "" {
domain = "https://" + bucket + "." + endpoint
}
b.Domain = strings.TrimRight(domain, "/")
b.Client, err = bos.NewClient(accessKey, secretKey, endpoint)
return
}
func (b *BOS) IsExist(object string) (err error) {
_, err = b.GetInfo(object)
return
}
func (b *BOS) Upload(tmpFile, saveFile string, headers ...map[string]string) (err error) {
var args = &api.PutObjectArgs{
UserMeta: make(map[string]string),
}
for _, header := range headers {
for k, v := range header {
switch strings.ToLower(k) {
case "content-disposition":
args.ContentDisposition = v
case "content-type":
args.ContentType = v
case "content-encoding":
args.ContentEncoding = v
default:
args.UserMeta[k] = v
}
}
}
_, err = b.Client.PutObjectFromFile(b.Bucket, objectRel(saveFile), tmpFile, args)
return
}
func (b *BOS) Delete(objects ...string) (err error) {
if len(objects) == 0 {
return
}
for idx, object := range objects {
objects[idx] = objectRel(object)
}
res, _ := b.Client.DeleteMultipleObjectsFromKeyList(b.Bucket, objects)
if res != nil && len(res.Errors) > 0 {
err = fmt.Errorf("%+v", res)
}
return
}
func (b *BOS) GetSignURL(object string, expire int64) (link string, err error) {
if expire <= 0 {
link = b.Domain + objectAbs(object)
} else {
link = b.Client.BasicGeneratePresignedUrl(b.Bucket, objectRel(object), int(expire))
if !strings.HasPrefix(link, b.Domain) {
if u, errU := url.Parse(link); errU == nil {
link = b.Domain + u.RequestURI()
}
}
}
return
}
func (b *BOS) Download(object string, savePath string) (err error) {
err = b.Client.DownloadSuperFile(b.Bucket, objectRel(object), savePath)
return
}
func (b *BOS) GetInfo(object string) (info File, err error) {
var resp *api.GetObjectMetaResult
resp, err = b.Client.GetObjectMeta(b.Bucket, objectRel(object))
if err != nil {
return
}
info = File{
Name: objectRel(object),
Size: resp.ContentLength,
IsDir: resp.ContentLength == 0,
Header: resp.UserMeta,
}
info.ModTime, _ = time.Parse(http.TimeFormat, resp.LastModified)
return
}
func (b *BOS) Lists(prefix string) (files []File, err error) {
var resp *api.ListObjectsResult
args := &api.ListObjectsArgs{
Prefix: objectRel(prefix),
MaxKeys: 1000,
}
resp, err = b.Client.ListObjects(b.Bucket, args)
if err != nil {
return
}
for _, object := range resp.Contents {
file := File{
Size: int64(object.Size),
Name: objectRel(object.Key),
IsDir: object.Size == 0,
}
file.ModTime, _ = time.Parse(http.TimeFormat, object.LastModified)
files = append(files, file)
}
return
}