forked from TruthHun/CloudStore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obs.go
180 lines (161 loc) · 3.64 KB
/
obs.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
package CloudStore
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"github.com/mmd1989/CloudStore/obs"
)
type OBS struct {
AccessKey string
SecretKey string
Bucket string
Endpoint string
Domain string
Client *obs.ObsClient
}
func NewOBS(accessKey, secretKey, bucket, endpoint, domain string) (o *OBS, err error) {
o = &OBS{
AccessKey: accessKey,
SecretKey: secretKey,
Endpoint: endpoint,
Bucket: bucket,
}
if domain == "" {
domain = fmt.Sprintf("https://%v.%v", bucket, endpoint)
}
o.Domain = strings.TrimRight(domain, "/")
o.Client, err = obs.New(accessKey, secretKey, endpoint)
return
}
func (o *OBS) IsExist(object string) (err error) {
_, err = o.GetInfo(object)
return
}
func (o *OBS) Upload(tmpFile, saveFile string, headers ...map[string]string) (err error) {
var p []byte
p, err = ioutil.ReadFile(tmpFile)
if err != nil {
return
}
input := &obs.PutObjectInput{}
input.Bucket = o.Bucket
input.Key = objectRel(saveFile)
input.Metadata = make(map[string]string)
input.Body = bytes.NewBuffer(p)
for _, header := range headers {
for k, v := range header {
switch strings.ToLower(k) {
case "content-type":
input.ContentType = v
case "content-encoding":
input.ContentEncoding = v
case "content-disposition":
input.ContentDisposition = v
default:
input.Metadata[k] = v
}
}
}
_, err = o.Client.PutObject(input)
return
}
func (o *OBS) Delete(objects ...string) (err error) {
if len(objects) <= 0 {
return
}
var objs []obs.ObjectToDelete
for _, object := range objects {
objs = append(objs, obs.ObjectToDelete{
Key: objectRel(object),
})
}
input := &obs.DeleteObjectsInput{
Bucket: o.Bucket,
Objects: objs,
}
_, err = o.Client.DeleteObjects(input)
return
}
func (o *OBS) GetSignURL(object string, expire int64) (link string, err error) {
if expire <= 0 {
link = o.Domain + objectAbs(object)
return
}
input := &obs.CreateSignedUrlInput{
Method: http.MethodGet,
Bucket: o.Bucket,
Key: objectRel(object),
Expires: int(expire),
}
output := &obs.CreateSignedUrlOutput{}
output, err = o.Client.CreateSignedUrl(input)
if err != nil {
return
}
link = output.SignedUrl
if !strings.HasPrefix(link, o.Domain) {
if u, errU := url.Parse(link); errU == nil {
link = o.Domain + u.RequestURI()
}
}
return
}
func (o *OBS) Download(object string, savePath string) (err error) {
input := &obs.GetObjectInput{}
input.Key = objectRel(object)
input.Bucket = o.Bucket
output := &obs.GetObjectOutput{}
output, err = o.Client.GetObject(input)
if err != nil {
return
}
defer output.Body.Close()
var b []byte
b, err = ioutil.ReadAll(output.Body)
if err != nil {
return
}
return ioutil.WriteFile(savePath, b, os.ModePerm)
}
func (o *OBS) GetInfo(object string) (info File, err error) {
input := &obs.GetObjectMetadataInput{
Bucket: o.Bucket,
Key: objectRel(object),
}
output := &obs.GetObjectMetadataOutput{}
output, err = o.Client.GetObjectMetadata(input)
if err != nil {
return
}
info = File{
Name: objectRel(object),
Size: output.ContentLength,
IsDir: output.ContentLength == 0,
ModTime: output.LastModified,
}
return
}
func (o *OBS) Lists(prefix string) (files []File, err error) {
prefix = objectRel(prefix)
input := &obs.ListObjectsInput{}
input.Prefix = prefix
input.Bucket = o.Bucket
output := &obs.ListObjectsOutput{}
output, err = o.Client.ListObjects(input)
if err != nil {
return
}
for _, item := range output.Contents {
files = append(files, File{
ModTime: item.LastModified,
Name: objectRel(item.Key),
Size: item.Size,
IsDir: item.Size == 0,
})
}
return
}