-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.go
43 lines (34 loc) · 936 Bytes
/
upload.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
package main
import (
"context"
"fmt"
"github.com/qiniu/go-sdk/v7/auth/qbox"
"github.com/qiniu/go-sdk/v7/storage"
)
type QiniuConfig struct {
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Bucket string `json:"bucket"`
FilePrefix string `json:"file_prefix"`
}
type QiniuUploader struct {
conf *QiniuConfig
}
func NewQiniuUploader(conf *QiniuConfig) Uploader {
return &QiniuUploader{conf: conf}
}
func (q *QiniuUploader) upload(key, filePath string) error {
putPolicy := storage.PutPolicy{
Scope: q.conf.Bucket,
}
mac := qbox.NewMac(q.conf.AccessKey, q.conf.SecretKey)
upToken := putPolicy.UploadToken(mac)
cfg := storage.Config{}
ret := storage.PutRet{}
formUploader := storage.NewFormUploader(&cfg)
err := formUploader.PutFile(context.Background(), &ret, upToken, key, filePath, nil)
if err != nil {
return fmt.Errorf("upload to qiniu failed: %w", err)
}
return nil
}