/
obs.go
74 lines (64 loc) · 1.53 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
package huawei
import (
"mime/multipart"
"github.com/baowk/dilu-plugin/file_store/config"
"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
"github.com/pkg/errors"
)
func New(cfg *config.FSCfg) *Obs {
return &Obs{
cfg: cfg,
}
}
type Obs struct {
cfg *config.FSCfg
}
func NewHuaWeiObsClient(cfg *config.FSCfg) (client *obs.ObsClient, err error) {
return obs.New(cfg.SecretID, cfg.SecretKey, cfg.Endpoint)
}
func (o *Obs) UploadFile(file *multipart.FileHeader) (filePath string, fileKey string, err error) {
// var open multipart.File
open, err := file.Open()
if err != nil {
return
}
defer open.Close()
fileKey = file.Filename
input := &obs.PutObjectInput{
PutObjectBasicInput: obs.PutObjectBasicInput{
ObjectOperationInput: obs.ObjectOperationInput{
Bucket: o.cfg.Bucket,
Key: fileKey,
},
//ContentType: file.Header.Get("content-type"),
},
Body: open,
}
var client *obs.ObsClient
client, err = NewHuaWeiObsClient(o.cfg)
if err != nil {
return
}
_, err = client.PutObject(input)
if err != nil {
return
}
filePath = o.cfg.PathPrefix + "/" + fileKey
return
}
func (o *Obs) DeleteFile(key string) error {
client, err := NewHuaWeiObsClient(o.cfg)
if err != nil {
return errors.Wrap(err, "获取华为对象存储对象失败!")
}
input := &obs.DeleteObjectInput{
Bucket: o.cfg.Bucket,
Key: key,
}
var output *obs.DeleteObjectOutput
output, err = client.DeleteObject(input)
if err != nil {
return errors.Wrapf(err, "删除对象(%s)失败!, output: %v", key, output)
}
return nil
}