-
Notifications
You must be signed in to change notification settings - Fork 0
/
qiniuyun.go
88 lines (74 loc) · 2.08 KB
/
qiniuyun.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
package util
import (
"context"
"crypto/md5"
"fmt"
"io"
"mime/multipart"
"strconv"
"time"
"github.com/qiniu/go-sdk/v7/auth/qbox"
"github.com/qiniu/go-sdk/v7/storage"
)
const (
accessKey = "ByyFFHWJCJRUaJpbquB9d8Nyxuc0O3sPp3waR6ku"
secretKey = "ea6nktNTugz795s4ZyNXtHsmPA-p5DhnIIhqAFRj"
bucket = "simple-tiktok"
Refer = "http://rp1950nfc.hn-bkt.clouddn.com/"
)
// 上传文件到七牛云
func UploadToQiNiu(file multipart.File, fileSize int64, filepath string) (string, Response) {
putPolicy := storage.PutPolicy{
Scope: bucket,
}
mac := qbox.NewMac(accessKey, secretKey)
upToken := putPolicy.UploadToken(mac)
// 指定地区,不使用cdn,https
cfg := storage.Config{
Zone: &storage.ZoneHuanan,
UseCdnDomains: false,
UseHTTPS: false,
}
putExtra := storage.PutExtra{}
formUploader := storage.NewFormUploader(&cfg)
ret := storage.PutRet{}
err := formUploader.Put(context.Background(), &ret, upToken, filepath, file, fileSize, &putExtra)
if err != nil {
fmt.Println(err)
return "Error", FileUploadErr
}
url := Refer + ret.Key
return url, Success
}
// 自动生成文件名
func GenFileName() string {
unix := time.Now().Unix()
timeByte := []byte(strconv.Itoa(int(unix)))
md5Str := md5.Sum(timeByte) // 这里是计算了一个md5(time())的字符串作为文件名
filename := fmt.Sprintf("%x", md5Str)
return filename
}
// 上传图片到七牛
func UploadImgToQiNiu(file io.Reader, fileSize int64, filepath string) (string, Response) {
putPolicy := storage.PutPolicy{
Scope: bucket,
}
mac := qbox.NewMac(accessKey, secretKey)
upToken := putPolicy.UploadToken(mac)
// 指定地区,不使用cdn,https
cfg := storage.Config{
Zone: &storage.ZoneHuanan,
UseCdnDomains: false,
UseHTTPS: false,
}
putExtra := storage.PutExtra{}
formUploader := storage.NewFormUploader(&cfg)
ret := storage.PutRet{}
err := formUploader.Put(context.Background(), &ret, upToken, filepath, file, fileSize, &putExtra)
if err != nil {
fmt.Println(err)
return "Error", FileUploadErr
}
url := Refer + ret.Key
return url, Success
}